Jewels and stones: Difference between revisions

Content added Content deleted
(add bqn)
m (syntax highlighting fixup automation)
Line 29: Line 29:
{{trans|Python}}
{{trans|Python}}


<lang 11l>F count_jewels(s, j)
<syntaxhighlight lang="11l">F count_jewels(s, j)
R sum(s.map(x -> Int(x C @j)))
R sum(s.map(x -> Int(x C @j)))


print(count_jewels(‘aAAbbbb’, ‘aA’))
print(count_jewels(‘aAAbbbb’, ‘aA’))
print(count_jewels(‘ZZ’, ‘z’))</lang>
print(count_jewels(‘ZZ’, ‘z’))</syntaxhighlight>


{{out}}
{{out}}
Line 43: Line 43:
=={{header|8080 Assembly}}==
=={{header|8080 Assembly}}==


<lang 8080asm> org 100h
<syntaxhighlight lang="8080asm"> org 100h
jmp demo
jmp demo
;;; Count jewels.
;;; Count jewels.
Line 105: Line 105:
;;; Next free page of memory is used for the jewel array
;;; Next free page of memory is used for the jewel array
jpage: equ $/256+1
jpage: equ $/256+1
jarr: equ jpage*256</lang>
jarr: equ jpage*256</syntaxhighlight>


{{out}}
{{out}}
Line 114: Line 114:
=={{header|8086 Assembly}}==
=={{header|8086 Assembly}}==


<lang asm> cpu 8086
<syntaxhighlight lang="asm"> cpu 8086
bits 16
bits 16
org 100h
org 100h
Line 167: Line 167:
num: db '$'
num: db '$'
stones: db 'aAAbbbb',0 ; Example from the task
stones: db 'aAAbbbb',0 ; Example from the task
jewels: db 'aA',0</lang>
jewels: db 'aA',0</syntaxhighlight>


{{out}}
{{out}}
Line 174: Line 174:


=={{header|Ada}}==
=={{header|Ada}}==
<lang Ada>with Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO;


procedure Jewels_And_Stones is
procedure Jewels_And_Stones is
Line 205: Line 205:
Show ("aAAbbbb", "aA");
Show ("aAAbbbb", "aA");
Show ("ZZ", "z");
Show ("ZZ", "z");
end Jewels_And_Stones;</lang>
end Jewels_And_Stones;</syntaxhighlight>


{{out}}
{{out}}
Line 212: Line 212:


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
<lang algol68>BEGIN
<syntaxhighlight lang="algol68">BEGIN
# procedure that counts the number of times the letters in jewels occur in stones #
# procedure that counts the number of times the letters in jewels occur in stones #
PROC count jewels = ( STRING stones, jewels )INT:
PROC count jewels = ( STRING stones, jewels )INT:
Line 248: Line 248:
print( ( count jewels( "ZZ", "z" ), newline ) )
print( ( count jewels( "ZZ", "z" ), newline ) )


END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre> +3
<pre> +3
Line 258: Line 258:
{{works with|Dyalog APL}}
{{works with|Dyalog APL}}


<lang apl>jewels ← +/∊⍨</lang>
<syntaxhighlight lang="apl">jewels ← +/∊⍨</syntaxhighlight>


{{out}}
{{out}}
Line 268: Line 268:
=={{header|AppleScript}}==
=={{header|AppleScript}}==
===Functional===
===Functional===
<lang applescript>-- jewelCount :: String -> String -> Int
<syntaxhighlight lang="applescript">-- jewelCount :: String -> String -> Int
on jewelCount(jewels, stones)
on jewelCount(jewels, stones)
set js to chars(jewels)
set js to chars(jewels)
Line 390: Line 390:
set my text item delimiters to dlm
set my text item delimiters to dlm
str
str
end unlines</lang>
end unlines</syntaxhighlight>
{{Out}}
{{Out}}
<pre>3
<pre>3
Line 397: Line 397:
===Idiomatic===
===Idiomatic===


<lang applescript>on jewelsAndStones(stones, jewels)
<syntaxhighlight lang="applescript">on jewelsAndStones(stones, jewels)
set counter to 0
set counter to 0
considering case
considering case
Line 408: Line 408:
end jewelsAndStones
end jewelsAndStones


jewelsAndStones("aAAbBbb", "aAb")</lang>
jewelsAndStones("aAAbBbb", "aAb")</syntaxhighlight>


{{output}}
{{output}}
<lang applescript>6</lang>
<syntaxhighlight lang="applescript">6</syntaxhighlight>


=={{header|Arturo}}==
=={{header|Arturo}}==
<lang rebol>count: function [jewels,stones][
<syntaxhighlight lang="rebol">count: function [jewels,stones][
size select split stones => [in? & jewels]
size select split stones => [in? & jewels]
]
]


print count "aA" "aAAbbbb"</lang>
print count "aA" "aAAbbbb"</syntaxhighlight>


{{out}}
{{out}}
Line 425: Line 425:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>JewelsandStones(ss, jj){
<syntaxhighlight lang="autohotkey">JewelsandStones(ss, jj){
for each, jewel in StrSplit(jj)
for each, jewel in StrSplit(jj)
for each, stone in StrSplit(ss)
for each, stone in StrSplit(ss)
Line 431: Line 431:
num++
num++
return num
return num
}</lang>
}</syntaxhighlight>
Example:<lang AutoHotkey>MsgBox % JewelsandStones("aAAbbbbz", "aAZ")
Example:<syntaxhighlight lang="autohotkey">MsgBox % JewelsandStones("aAAbbbbz", "aAZ")
return</lang>
return</syntaxhighlight>
Outputs:<pre>3</pre>
Outputs:<pre>3</pre>


=={{header|AWK}}==
=={{header|AWK}}==
<lang AWK># syntax: GAWK -f JEWELS_AND_STONES.AWK
<syntaxhighlight lang="awk"># syntax: GAWK -f JEWELS_AND_STONES.AWK
BEGIN {
BEGIN {
printf("%d\n",count("aAAbbbb","aA"))
printf("%d\n",count("aAAbbbb","aA"))
Line 451: Line 451:
return(total)
return(total)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>3
<pre>3
Line 458: Line 458:
=={{header|BASIC}}==
=={{header|BASIC}}==


<lang basic>10 READ N%
<syntaxhighlight lang="basic">10 READ N%
20 FOR A%=1 TO N%
20 FOR A%=1 TO N%
30 READ J$,S$
30 READ J$,S$
Line 477: Line 477:
210 DATA "aA","aAAbbbb"
210 DATA "aA","aAAbbbb"
220 DATA "z","ZZZZ"
220 DATA "z","ZZZZ"
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 485: Line 485:


=={{header|BASIC256}}==
=={{header|BASIC256}}==
<syntaxhighlight lang="basic256">
<lang BASIC256>
function contar_joyas(piedras, joyas)
function contar_joyas(piedras, joyas)
cont = 0
cont = 0
Line 499: Line 499:
print contar_joyas("ABCDEFGHIJKLMNOPQRSTUVWXYZ@abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ@abcdefghijklmnopqrstuvwxyz")
print contar_joyas("ABCDEFGHIJKLMNOPQRSTUVWXYZ@abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ@abcdefghijklmnopqrstuvwxyz")
print contar_joyas("AB", "")
print contar_joyas("AB", "")
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 506: Line 506:


=={{header|BCPL}}==
=={{header|BCPL}}==
<lang bcpl>get "libhdr"
<syntaxhighlight lang="bcpl">get "libhdr"


let jewels(j, s) = valof
let jewels(j, s) = valof
Line 525: Line 525:
$( show("aA", "aAAbbbb")
$( show("aA", "aAAbbbb")
show("z", "ZZ")
show("z", "ZZ")
$)</lang>
$)</syntaxhighlight>
{{out}}
{{out}}
<pre>"aA" in "aAAbbbb": 3
<pre>"aA" in "aAAbbbb": 3
Line 533: Line 533:
Similar in nature to APL, mostly due to how trivial the problem is in an array language.
Similar in nature to APL, mostly due to how trivial the problem is in an array language.


<lang bqn>Jewels←+´∊˜</lang>
<syntaxhighlight lang="bqn">Jewels←+´∊˜</syntaxhighlight>
<lang> "aA" Jewels "aAAbbbb"
<syntaxhighlight lang="text"> "aA" Jewels "aAAbbbb"
3</lang>
3</syntaxhighlight>
=={{header|Bracmat}}==
=={{header|Bracmat}}==
<lang Bracmat> ( f
<syntaxhighlight lang="bracmat"> ( f
= stones jewels N
= stones jewels N
. !arg:(?stones.?jewels)
. !arg:(?stones.?jewels)
Line 556: Line 556:
)
)
& f$(aAAbbbb.aA)
& f$(aAAbbbb.aA)
</syntaxhighlight>
</lang>
'''Output'''
'''Output'''
<pre>3</pre>
<pre>3</pre>
Line 562: Line 562:
=={{header|C}}==
=={{header|C}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
#include <string.h>


Line 575: Line 575:
printf("%d\n", count_jewels("ZZ", "z"));
printf("%d\n", count_jewels("ZZ", "z"));
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{output}}
{{output}}
Line 584: Line 584:


=={{header|C sharp}}==
=={{header|C sharp}}==
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Linq;
using System.Linq;


Line 598: Line 598:
return stones.Count(bag.Contains);
return stones.Count(bag.Contains);
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 607: Line 607:
=={{header|C++}}==
=={{header|C++}}==
{{trans|D}}
{{trans|D}}
<lang cpp>#include <algorithm>
<syntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <iostream>


Line 627: Line 627:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>3
<pre>3
Line 633: Line 633:


=={{header|CLU}}==
=={{header|CLU}}==
<lang clu>count_jewels = proc (jewels, stones: string) returns (int)
<syntaxhighlight lang="clu">count_jewels = proc (jewels, stones: string) returns (int)
is_jewel: array[bool] := array[bool]$fill(0, 256, false)
is_jewel: array[bool] := array[bool]$fill(0, 256, false)
for c: char in string$chars(jewels) do
for c: char in string$chars(jewels) do
Line 655: Line 655:
start_up = proc ()
start_up = proc ()
show("aA", "aAAbbbb")
show("aA", "aAAbbbb")
end start_up</lang>
end start_up</syntaxhighlight>
{{out}}
{{out}}
<pre>"aA" in "aAAbbbb": 3</pre>
<pre>"aA" in "aAAbbbb": 3</pre>


=={{header|Cowgol}}==
=={{header|Cowgol}}==
<lang cowgol>include "cowgol.coh";
<syntaxhighlight lang="cowgol">include "cowgol.coh";


sub count_jewels(stones: [uint8], jewels: [uint8]): (count: uint16) is
sub count_jewels(stones: [uint8], jewels: [uint8]): (count: uint16) is
Line 688: Line 688:


print_and_count("aAAbbbb", "aA");
print_and_count("aAAbbbb", "aA");
print_and_count("ZZ", "z");</lang>
print_and_count("ZZ", "z");</syntaxhighlight>


{{out}}
{{out}}
Line 696: Line 696:


=={{header|Crystal}}==
=={{header|Crystal}}==
<lang ruby>stones, jewels = "aAAbbbb", "aA"
<syntaxhighlight lang="ruby">stones, jewels = "aAAbbbb", "aA"
stones.count(jewels) # => 3
stones.count(jewels) # => 3


Line 705: Line 705:


# This works as intended though:
# This works as intended though:
stones.count { |c| jewels.chars.includes?(c) } # => 2</lang>
stones.count { |c| jewels.chars.includes?(c) } # => 2</syntaxhighlight>


=={{header|D}}==
=={{header|D}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang d>import std.algorithm;
<syntaxhighlight lang="d">import std.algorithm;
import std.stdio;
import std.stdio;


Line 725: Line 725:
countJewels("aAAbbbb", "aA").writeln;
countJewels("aAAbbbb", "aA").writeln;
countJewels("ZZ", "z").writeln;
countJewels("ZZ", "z").writeln;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>3
<pre>3
Line 731: Line 731:


=={{header|Draco}}==
=={{header|Draco}}==
<lang draco>proc nonrec count_jewels(*char jewels, stones) word:
<syntaxhighlight lang="draco">proc nonrec count_jewels(*char jewels, stones) word:
[256] bool jewel;
[256] bool jewel;
word count;
word count;
Line 758: Line 758:
show("aA", "aAAbbbb");
show("aA", "aAAbbbb");
show("z", "ZZ")
show("z", "ZZ")
corp</lang>
corp</syntaxhighlight>
{{out}}
{{out}}
<pre>'aA' in 'aAAbbbb': 3
<pre>'aA' in 'aAAbbbb': 3
Line 767: Line 767:
{{trans|Swift}}
{{trans|Swift}}


<lang dyalect>func countJewels(stones, jewels) {
<syntaxhighlight lang="dyalect">func countJewels(stones, jewels) {
stones.Iterate().Map(x => jewels.Contains(x) ? 1 : 0).Reduce((x,y) => x + y, 0)
stones.Iterate().Map(x => jewels.Contains(x) ? 1 : 0).Reduce((x,y) => x + y, 0)
}
}
print(countJewels("aAAbbbb", "aA"))
print(countJewels("aAAbbbb", "aA"))
print(countJewels("ZZ", "z"))</lang>
print(countJewels("ZZ", "z"))</syntaxhighlight>


{{out}}
{{out}}
Line 780: Line 780:


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>
<syntaxhighlight lang="fsharp">
let fN jewels stones=stones|>Seq.filter(fun n->Seq.contains n jewels)|>Seq.length
let fN jewels stones=stones|>Seq.filter(fun n->Seq.contains n jewels)|>Seq.length
printfn "%d" (fN "aA" "aAAbbbb")
printfn "%d" (fN "aA" "aAAbbbb")
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 790: Line 790:


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: kernel prettyprint sequences ;
<syntaxhighlight lang="factor">USING: kernel prettyprint sequences ;


: count-jewels ( stones jewels -- n ) [ member? ] curry count ;
: count-jewels ( stones jewels -- n ) [ member? ] curry count ;


"aAAbbbb" "aA"
"aAAbbbb" "aA"
"ZZ" "z" [ count-jewels . ] 2bi@</lang>
"ZZ" "z" [ count-jewels . ] 2bi@</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 803: Line 803:


=={{header|Euphoria}}==
=={{header|Euphoria}}==
<syntaxhighlight lang="euphoria">
<lang Euphoria>
function number_of(object jewels, object stones) -- why limit ourselves to strings?
function number_of(object jewels, object stones) -- why limit ourselves to strings?
integer ct = 0
integer ct = 0
Line 815: Line 815:
? number_of("z","ZZ")
? number_of("z","ZZ")
? number_of({1,"Boo",3},{1,2,3,'A',"Boo",3}) -- might as well send a list of things to find, not just one!
? number_of({1,"Boo",3},{1,2,3,'A',"Boo",3}) -- might as well send a list of things to find, not just one!
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 824: Line 824:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>
<syntaxhighlight lang="freebasic">
function contar_joyas(piedras as string, joyas as string) as integer
function contar_joyas(piedras as string, joyas as string) as integer
dim as integer bc, cont: cont = 0
dim as integer bc, cont: cont = 0
Line 839: Line 839:
"ABCDEFGHIJKLMNOPQRSTUVWXYZ@abcdefghijklmnopqrstuvwxyz")
"ABCDEFGHIJKLMNOPQRSTUVWXYZ@abcdefghijklmnopqrstuvwxyz")
print contar_joyas("AB", "")
print contar_joyas("AB", "")
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 854: Line 854:


'''Outer loop stones, index into jewels:'''
'''Outer loop stones, index into jewels:'''
<lang go>package main
<syntaxhighlight lang="go">package main
import (
import (
Line 872: Line 872:
func main() {
func main() {
fmt.Println(js("aAAbbbb", "aA"))
fmt.Println(js("aAAbbbb", "aA"))
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>3</pre>
<pre>3</pre>


'''Outer loop jewels, count stones:'''
'''Outer loop jewels, count stones:'''
<lang go>func js(stones, jewels string) (n int) {
<syntaxhighlight lang="go">func js(stones, jewels string) (n int) {
for _, b := range []byte(jewels) {
for _, b := range []byte(jewels) {
n += strings.Count(stones, string(b))
n += strings.Count(stones, string(b))
}
}
return
return
}</lang>
}</syntaxhighlight>


'''Construct jewel set, then loop over stones:'''
'''Construct jewel set, then loop over stones:'''
<lang go>func js(stones, jewels string) (n int) {
<syntaxhighlight lang="go">func js(stones, jewels string) (n int) {
var jSet ['z' + 1]int
var jSet ['z' + 1]int
for _, b := range []byte(jewels) {
for _, b := range []byte(jewels) {
Line 894: Line 894:
}
}
return
return
}</lang>
}</syntaxhighlight>


'''Construct stone multiset, then loop over jewels:'''
'''Construct stone multiset, then loop over jewels:'''
<lang go>func js(stones, jewels string) (n int) {
<syntaxhighlight lang="go">func js(stones, jewels string) (n int) {
var sset ['z' + 1]int
var sset ['z' + 1]int
for _, b := range []byte(stones) {
for _, b := range []byte(stones) {
Line 906: Line 906:
}
}
return
return
}</lang>
}</syntaxhighlight>


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>jewelCount
<syntaxhighlight lang="haskell">jewelCount
:: Eq a
:: Eq a
=> [a] -> [a] -> Int
=> [a] -> [a] -> Int
Line 921: Line 921:
main :: IO ()
main :: IO ()
main = mapM_ print $ uncurry jewelCount <$> [("aA", "aAAbbbb"), ("z", "ZZ")]
main = mapM_ print $ uncurry jewelCount <$> [("aA", "aAAbbbb"), ("z", "ZZ")]
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>3
<pre>3
Line 928: Line 928:
Or in terms of filter rather than foldr
Or in terms of filter rather than foldr


<lang haskell>jewelCount
<syntaxhighlight lang="haskell">jewelCount
:: Eq a
:: Eq a
=> [a] -> [a] -> Int
=> [a] -> [a] -> Int
Line 937: Line 937:
main = do
main = do
print $ jewelCount "aA" "aAAbbbb"
print $ jewelCount "aA" "aAAbbbb"
print $ jewelCount "z" "ZZ"</lang>
print $ jewelCount "z" "ZZ"</syntaxhighlight>
{{Out}}
{{Out}}
<pre>3
<pre>3
Line 943: Line 943:


=={{header|J}}==
=={{header|J}}==
<syntaxhighlight lang="j">
<lang J>
NB. jewels sums a raveled equality table
NB. jewels sums a raveled equality table
NB. use: x jewels y x are the stones, y are the jewels.
NB. use: x jewels y x are the stones, y are the jewels.
Line 957: Line 957:
0
0
</syntaxhighlight>
</lang>


=={{header|Java}}==
=={{header|Java}}==
<lang java>import java.util.HashSet;
<syntaxhighlight lang="java">import java.util.HashSet;
import java.util.Set;
import java.util.Set;


Line 984: Line 984:
System.out.println(countJewels("ZZ", "z"));
System.out.println(countJewels("ZZ", "z"));
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>3
<pre>3
Line 990: Line 990:


=={{header|Javascript}}==
=={{header|Javascript}}==
<lang javascript>(() => {
<syntaxhighlight lang="javascript">(() => {


// jewelCount :: String -> String -> Int
// jewelCount :: String -> String -> Int
Line 1,005: Line 1,005:
]
]
.map(x => jewelCount(...x))
.map(x => jewelCount(...x))
})();</lang>
})();</syntaxhighlight>
{{Out}}
{{Out}}
<pre>[3, 0]</pre>
<pre>[3, 0]</pre>


=={{header|jq}}==
=={{header|jq}}==
<lang jq>$ jq -n --arg stones aAAbbbb --arg jewels aA '
<syntaxhighlight lang="jq">$ jq -n --arg stones aAAbbbb --arg jewels aA '
[$stones|split("") as $s|$jewels|split("") as $j|$s[]|
[$stones|split("") as $s|$jewels|split("") as $j|$s[]|
select(. as $c|$j|contains([$c]))]|length'</lang>
select(. as $c|$j|contains([$c]))]|length'</syntaxhighlight>


{{Out}}
{{Out}}
Line 1,020: Line 1,020:


'''Module''':
'''Module''':
<lang julia>module Jewels
<syntaxhighlight lang="julia">module Jewels


count(s, j) = Base.count(x ∈ j for x in s)
count(s, j) = Base.count(x ∈ j for x in s)


end # module Jewels</lang>
end # module Jewels</syntaxhighlight>


'''Main''':
'''Main''':
<lang julia>@show Jewels.count("aAAbbbb", "aA")
<syntaxhighlight lang="julia">@show Jewels.count("aAAbbbb", "aA")
@show Jewels.count("ZZ", "z")</lang>
@show Jewels.count("ZZ", "z")</syntaxhighlight>


{{out}}
{{out}}
Line 1,035: Line 1,035:


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>// Version 1.2.40
<syntaxhighlight lang="scala">// Version 1.2.40


fun countJewels(s: String, j: String) = s.count { it in j }
fun countJewels(s: String, j: String) = s.count { it in j }
Line 1,042: Line 1,042:
println(countJewels("aAAbbbb", "aA"))
println(countJewels("aAAbbbb", "aA"))
println(countJewels("ZZ", "z"))
println(countJewels("ZZ", "z"))
}</lang>
}</syntaxhighlight>


{{output}}
{{output}}
Line 1,051: Line 1,051:


=={{header|Lambdatalk}}==
=={{header|Lambdatalk}}==
<lang scheme>
<syntaxhighlight lang="scheme">
{def countjewels
{def countjewels
{def countjewels.r
{def countjewels.r
Line 1,068: Line 1,068:
{countjewels aAAbbbb aA} -> 3
{countjewels aAAbbbb aA} -> 3
{countjewels ZZ z} -> 0
{countjewels ZZ z} -> 0
</syntaxhighlight>
</lang>


=={{header|Lua}}==
=={{header|Lua}}==
{{trans|C}}
{{trans|C}}
<lang lua>function count_jewels(s, j)
<syntaxhighlight lang="lua">function count_jewels(s, j)
local count = 0
local count = 0
for i=1,#s do
for i=1,#s do
Line 1,084: Line 1,084:


print(count_jewels("aAAbbbb", "aA"))
print(count_jewels("aAAbbbb", "aA"))
print(count_jewels("ZZ", "z"))</lang>
print(count_jewels("ZZ", "z"))</syntaxhighlight>
{{out}}
{{out}}
<pre>3
<pre>3
Line 1,090: Line 1,090:


=={{header|Maple}}==
=={{header|Maple}}==
<lang Maple>count_jewel := proc(stones, jewels)
<syntaxhighlight lang="maple">count_jewel := proc(stones, jewels)
local count, j, letter:
local count, j, letter:
j := convert(jewels,set):
j := convert(jewels,set):
Line 1,101: Line 1,101:
return count:
return count:
end proc:
end proc:
count_jewel("aAAbbbb", "aA")</lang>
count_jewel("aAAbbbb", "aA")</syntaxhighlight>
{{Out|Output}}
{{Out|Output}}
<pre>3</pre>
<pre>3</pre>


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>JewelsStones[j_String, s_String] := Count[MemberQ[Characters[j], #] & /@ Characters[s], True]
<syntaxhighlight lang="mathematica">JewelsStones[j_String, s_String] := Count[MemberQ[Characters[j], #] & /@ Characters[s], True]
JewelsStones["aA", "aAAbbbb"]
JewelsStones["aA", "aAAbbbb"]
JewelsStones["ZZ", "z"]</lang>
JewelsStones["ZZ", "z"]</syntaxhighlight>
{{out}}
{{out}}
<pre>3
<pre>3
Line 1,114: Line 1,114:


=={{header|MATLAB}} / {{header|Octave}}==
=={{header|MATLAB}} / {{header|Octave}}==
<syntaxhighlight lang="matlab">
<lang Matlab>
function s = count_jewels(stones,jewels)
function s = count_jewels(stones,jewels)
s=0;
s=0;
Line 1,124: Line 1,124:
%!test
%!test
%! assert(count_jewels('ZZ','z'),0)
%! assert(count_jewels('ZZ','z'),0)
</syntaxhighlight>
</lang>


=={{header|min}}==
=={{header|min}}==
{{works with|min|0.19.6}}
{{works with|min|0.19.6}}
<lang min>(('' '' '') => spread if) :if?
<syntaxhighlight lang="min">(('' '' '') => spread if) :if?


((1 0 if?) concat map sum) :count
((1 0 if?) concat map sum) :count
Line 1,137: Line 1,137:


"aAAbbbb" "aA" count-jewels puts!
"aAAbbbb" "aA" count-jewels puts!
"ZZ" "z" count-jewels puts!</lang>
"ZZ" "z" count-jewels puts!</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,145: Line 1,145:


=={{header|Modula-2}}==
=={{header|Modula-2}}==
<lang modula2>MODULE Jewels;
<syntaxhighlight lang="modula2">MODULE Jewels;
FROM FormatString IMPORT FormatString;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 1,180: Line 1,180:


ReadChar
ReadChar
END Jewels.</lang>
END Jewels.</syntaxhighlight>
{{out}}
{{out}}
<pre>3
<pre>3
Line 1,186: Line 1,186:


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>import sequtils
<syntaxhighlight lang="nim">import sequtils


func countJewels(stones, jewels: string): Natural =
func countJewels(stones, jewels: string): Natural =
Line 1,192: Line 1,192:


echo countJewels("aAAbbbb", "aA")
echo countJewels("aAAbbbb", "aA")
echo countJewels("ZZ", "z")</lang>
echo countJewels("ZZ", "z")</syntaxhighlight>


{{out}}
{{out}}
Line 1,200: Line 1,200:
=={{header|Objeck}}==
=={{header|Objeck}}==
{{trans|Java}}
{{trans|Java}}
<lang Objeck>use Collection.Generic;
<syntaxhighlight lang="objeck">use Collection.Generic;


class JewelsStones {
class JewelsStones {
Line 1,224: Line 1,224:
return count;
return count;
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,233: Line 1,233:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>sub count_jewels {
<syntaxhighlight lang="perl">sub count_jewels {
my( $j, $s ) = @_;
my( $j, $s ) = @_;
my($c,%S);
my($c,%S);
Line 1,243: Line 1,243:


print count_jewels 'aA' , 'aAAbbbb';
print count_jewels 'aA' , 'aAAbbbb';
print count_jewels 'z' , 'ZZ';</lang>
print count_jewels 'z' , 'ZZ';</syntaxhighlight>
{{out}}
{{out}}
<pre>3
<pre>3
0</pre>
0</pre>
===Alternate using regex===
===Alternate using regex===
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl


use strict; # https://rosettacode.org/wiki/Jewels_and_Stones#Perl
use strict; # https://rosettacode.org/wiki/Jewels_and_Stones#Perl
Line 1,259: Line 1,259:
aAAbbbb abc
aAAbbbb abc
ZZ z
ZZ z
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,268: Line 1,268:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">count_jewels</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">stones</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">jewels</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">count_jewels</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">stones</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">jewels</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
Line 1,278: Line 1,278:
<span style="color: #0000FF;">?</span><span style="color: #000000;">count_jewels</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"aAAbbbb"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"aA"</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">count_jewels</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"aAAbbbb"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"aA"</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">count_jewels</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"ZZ"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"z"</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">count_jewels</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"ZZ"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"z"</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,288: Line 1,288:
===List comprehension===
===List comprehension===


<lang Picat>jewels_and_stones1(Jewels,Stones) = sum([1 : S in Stones, J in Jewels, S == J]).</lang>
<syntaxhighlight lang="picat">jewels_and_stones1(Jewels,Stones) = sum([1 : S in Stones, J in Jewels, S == J]).</syntaxhighlight>


===Recursion===
===Recursion===
<lang Picat>jewels_and_stones2(Jewels,Stones) = N =>
<syntaxhighlight lang="picat">jewels_and_stones2(Jewels,Stones) = N =>
jewels_and_stones2(Jewels,Stones,0,N).
jewels_and_stones2(Jewels,Stones,0,N).


Line 1,307: Line 1,307:
N1 = N0
N1 = N0
),
),
jewels_and_stones2_(J,Stones,N1,N).</lang>
jewels_and_stones2_(J,Stones,N1,N).</syntaxhighlight>


===Foreach loop===
===Foreach loop===
<lang Picat>jewels_and_stones3(Jewels,Stones) = N =>
<syntaxhighlight lang="picat">jewels_and_stones3(Jewels,Stones) = N =>
N = 0,
N = 0,
foreach(J in Jewels, S in Stones)
foreach(J in Jewels, S in Stones)
Line 1,316: Line 1,316:
N := N + 1
N := N + 1
end
end
end.</lang>
end.</syntaxhighlight>


===Test===
===Test===
<lang Picat>go =>
<syntaxhighlight lang="picat">go =>
Tests = [["aA","aAAbbbb"],
Tests = [["aA","aAAbbbb"],
["z","ZZ"]
["z","ZZ"]
Line 1,331: Line 1,331:
nl
nl
end,
end,
nl.</lang>
nl.</syntaxhighlight>


{{out}}
{{out}}
Line 1,347: Line 1,347:
===Benchmark===
===Benchmark===
For a larger test we can see the differences between the three approaches. Here are 100 000 000 random stones and (atmost) 15 jewels (we remove any duplicate jewel).
For a larger test we can see the differences between the three approaches. Here are 100 000 000 random stones and (atmost) 15 jewels (we remove any duplicate jewel).
<lang Picat>go2 =>
<syntaxhighlight lang="picat">go2 =>
Alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
Alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
Len = Alpha.len,
Len = Alpha.len,
Line 1,361: Line 1,361:
time(println(js2=jewels_and_stones2(Jewels,Stones))),
time(println(js2=jewels_and_stones2(Jewels,Stones))),
time(println(js3=jewels_and_stones3(Jewels,Stones))),
time(println(js3=jewels_and_stones3(Jewels,Stones))),
nl.</lang>
nl.</syntaxhighlight>


{{out}}
{{out}}
Line 1,383: Line 1,383:


=={{header|PL/M}}==
=={{header|PL/M}}==
<lang plm>100H:
<syntaxhighlight lang="plm">100H:


/* FIND JEWELS AMONG STONES */
/* FIND JEWELS AMONG STONES */
Line 1,458: Line 1,458:


CALL BDOS(0,0);
CALL BDOS(0,0);
EOF</lang>
EOF</syntaxhighlight>
{{out}}
{{out}}
<pre>'aA' IN 'aAAbbbb': 3
<pre>'aA' IN 'aAAbbbb': 3
Line 1,465: Line 1,465:
=={{header|Prolog}}==
=={{header|Prolog}}==


<lang prolog>
<syntaxhighlight lang="prolog">


:- system:set_prolog_flag(double_quotes,codes) .
:- system:set_prolog_flag(double_quotes,codes) .
Line 1,475: Line 1,475:
.
.


</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,496: Line 1,496:


{{works with|SWI Prolog}}
{{works with|SWI Prolog}}
<lang prolog>count_jewels(Stones, Jewels, N):-
<syntaxhighlight lang="prolog">count_jewels(Stones, Jewels, N):-
string_codes(Stones, Scodes),
string_codes(Stones, Scodes),
string_codes(Jewels, Jcodes),
string_codes(Jewels, Jcodes),
Line 1,514: Line 1,514:
count_jewels([S|Stones], Jewels, N, R).
count_jewels([S|Stones], Jewels, N, R).
count_jewels([_|Stones], Jewels, N, R):-
count_jewels([_|Stones], Jewels, N, R):-
count_jewels(Stones, Jewels, N, R).</lang>
count_jewels(Stones, Jewels, N, R).</syntaxhighlight>


{{out}}
{{out}}
Line 1,530: Line 1,530:


=={{header|Python}}==
=={{header|Python}}==
<lang python>def countJewels(s, j):
<syntaxhighlight lang="python">def countJewels(s, j):
return sum(x in j for x in s)
return sum(x in j for x in s)


print countJewels("aAAbbbb", "aA")
print countJewels("aAAbbbb", "aA")
print countJewels("ZZ", "z")</lang>
print countJewels("ZZ", "z")</syntaxhighlight>
{{Out}}
{{Out}}
<pre>3
<pre>3
Line 1,540: Line 1,540:


===Python 3 Alternative===
===Python 3 Alternative===
<lang python>def countJewels(stones, jewels):
<syntaxhighlight lang="python">def countJewels(stones, jewels):
jewelset = set(jewels)
jewelset = set(jewels)
return sum(1 for stone in stones if stone in jewelset)
return sum(1 for stone in stones if stone in jewelset)


print(countJewels("aAAbbbb", "aA"))
print(countJewels("aAAbbbb", "aA"))
print(countJewels("ZZ", "z"))</lang>
print(countJewels("ZZ", "z"))</syntaxhighlight>
{{Out}}
{{Out}}
<pre>3
<pre>3
Line 1,551: Line 1,551:


=={{header|R}}==
=={{header|R}}==
<lang R>J_n_S <- function(stones ="aAAbbbb", jewels = "aA") {
<syntaxhighlight lang="r">J_n_S <- function(stones ="aAAbbbb", jewels = "aA") {
stones <- unlist(strsplit(stones, split = "")) # obtain a character vector
stones <- unlist(strsplit(stones, split = "")) # obtain a character vector
jewels <- unlist(strsplit(jewels, split = ""))
jewels <- unlist(strsplit(jewels, split = ""))
Line 1,560: Line 1,560:
print(J_n_S("ZZ", "z"))
print(J_n_S("ZZ", "z"))
print(J_n_S("lgGKJGljglghGLGHlhglghoIPOgfdtrdDCHnvbnmBVC", "fFgGhH"))
print(J_n_S("lgGKJGljglghGLGHlhglghoIPOgfdtrdDCHnvbnmBVC", "fFgGhH"))
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>> print(J_n_S("aAAbbbb", "aA"))
<pre>> print(J_n_S("aAAbbbb", "aA"))
Line 1,572: Line 1,572:
=={{header|Quackery}}==
=={{header|Quackery}}==


<lang Quackery> [ 0 0 rot
<syntaxhighlight lang="quackery"> [ 0 0 rot
witheach [ bit | ]
witheach [ bit | ]
rot witheach
rot witheach
Line 1,579: Line 1,579:
drop ] is j&s ( $ $ --> n )
drop ] is j&s ( $ $ --> n )


$ "aAAbbbb" $ "aA" j&s echo</lang>
$ "aAAbbbb" $ "aA" j&s echo</syntaxhighlight>


{{out}}
{{out}}
Line 1,586: Line 1,586:


=={{header|Racket}}==
=={{header|Racket}}==
<lang racket>#lang racket
<syntaxhighlight lang="racket">#lang racket


(define (jewels-and-stones stones jewels)
(define (jewels-and-stones stones jewels)
Line 1,594: Line 1,594:
(jewels-and-stones "aAAbbbb" "aA")
(jewels-and-stones "aAAbbbb" "aA")
(jewels-and-stones "ZZ" "z"))
(jewels-and-stones "ZZ" "z"))
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>3
<pre>3
Line 1,601: Line 1,601:
=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
<lang perl6>sub count-jewels ( Str $j, Str $s --> Int ) {
<syntaxhighlight lang="raku" line>sub count-jewels ( Str $j, Str $s --> Int ) {
my %counts_of_all = $s.comb.Bag;
my %counts_of_all = $s.comb.Bag;
my @jewel_list = $j.comb.unique;
my @jewel_list = $j.comb.unique;
Line 1,609: Line 1,609:


say count-jewels 'aA' , 'aAAbbbb';
say count-jewels 'aA' , 'aAAbbbb';
say count-jewels 'z' , 'ZZ';</lang>
say count-jewels 'z' , 'ZZ';</syntaxhighlight>
{{Out}}
{{Out}}
<pre>3
<pre>3
Line 1,615: Line 1,615:


=={{header|Red}}==
=={{header|Red}}==
<lang rebol>Red [
<syntaxhighlight lang="rebol">Red [
title: "Jewels and stones"
title: "Jewels and stones"
red-version: 0.6.4
red-version: 0.6.4
Line 1,642: Line 1,642:


print count-jewels "aAAbbbb" "aA"
print count-jewels "aAAbbbb" "aA"
print count-jewels "ZZ" "z"</lang>
print count-jewels "ZZ" "z"</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,651: Line 1,651:
=={{header|REXX}}==
=={{header|REXX}}==
Programming note: &nbsp; a check is made so that only (Latin) letters are counted as a match.
Programming note: &nbsp; a check is made so that only (Latin) letters are counted as a match.
<lang rexx>/*REXX pgm counts how many letters (in the 1st string) are in common with the 2nd string*/
<syntaxhighlight lang="rexx">/*REXX pgm counts how many letters (in the 1st string) are in common with the 2nd string*/
say count('aAAbbbb', "aA")
say count('aAAbbbb', "aA")
say count('ZZ' , "z" )
say count('ZZ' , "z" )
Line 1,662: Line 1,662:
if datatype(x, 'M') then if pos(x, jewels)\==0 then #= # + 1
if datatype(x, 'M') then if pos(x, jewels)\==0 then #= # + 1
end /*j*/ /* [↑] if a letter and a match, bump #*/
end /*j*/ /* [↑] if a letter and a match, bump #*/
return # /*return the number of common letters. */</lang>
return # /*return the number of common letters. */</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>3
<pre>3
Line 1,668: Line 1,668:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring># Project Jewels and Stones
<syntaxhighlight lang="ring"># Project Jewels and Stones
jewels = "aA"
jewels = "aA"
Line 1,686: Line 1,686:
next
next
return num
return num
</syntaxhighlight>
</lang>
Output:
Output:
<pre>3
<pre>3
Line 1,692: Line 1,692:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>stones, jewels = "aAAbbbb", "aA"
<syntaxhighlight lang="ruby">stones, jewels = "aAAbbbb", "aA"


stones.count(jewels) # => 3
stones.count(jewels) # => 3
</syntaxhighlight>
</lang>


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>fn count_jewels(stones: &str, jewels: &str) -> u8 {
<syntaxhighlight lang="rust">fn count_jewels(stones: &str, jewels: &str) -> u8 {
let mut count: u8 = 0;
let mut count: u8 = 0;
for cur_char in stones.chars() {
for cur_char in stones.chars() {
Line 1,711: Line 1,711:
println!("{}", count_jewels("ZZ", "z"));
println!("{}", count_jewels("ZZ", "z"));
}
}
</syntaxhighlight>
</lang>
Output:<pre>3
Output:<pre>3
0</pre>
0</pre>


=={{header|Scala}}==
=={{header|Scala}}==
<lang Scala>object JewelsStones extends App {
<syntaxhighlight lang="scala">object JewelsStones extends App {
def countJewels(s: String, j: String): Int = s.count(i => j.contains(i))
def countJewels(s: String, j: String): Int = s.count(i => j.contains(i))


println(countJewels("aAAbbbb", "aA"))
println(countJewels("aAAbbbb", "aA"))
println(countJewels("ZZ", "z"))
println(countJewels("ZZ", "z"))
}</lang>
}</syntaxhighlight>
{{Out}}See it in running in your browser by [https://scalafiddle.io/sf/Cz1HXAT/0 ScalaFiddle (JavaScript)] or by [https://scastie.scala-lang.org/7ZCCN5hISRuDqLWTKVBHow Scastie (JVM)].
{{Out}}See it in running in your browser by [https://scalafiddle.io/sf/Cz1HXAT/0 ScalaFiddle (JavaScript)] or by [https://scastie.scala-lang.org/7ZCCN5hISRuDqLWTKVBHow Scastie (JVM)].


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>func countJewels(s, j) {
<syntaxhighlight lang="ruby">func countJewels(s, j) {
s.chars.count { |c|
s.chars.count { |c|
j.contains(c)
j.contains(c)
Line 1,732: Line 1,732:


say countJewels("aAAbbbb", "aA") #=> 3
say countJewels("aAAbbbb", "aA") #=> 3
say countJewels("ZZ", "z") #=> 0</lang>
say countJewels("ZZ", "z") #=> 0</syntaxhighlight>


=={{header|Snobol}}==
=={{header|Snobol}}==
<lang snobol>* See how many jewels are among the stones
<syntaxhighlight lang="snobol">* See how many jewels are among the stones
DEFINE('JEWELS(JWL,STN)') :(JEWELS_END)
DEFINE('JEWELS(JWL,STN)') :(JEWELS_END)
JEWELS JEWELS = 0
JEWELS JEWELS = 0
Line 1,747: Line 1,747:
* Example with no jewels (prints 0)
* Example with no jewels (prints 0)
OUTPUT = JEWELS('z','ZZ')
OUTPUT = JEWELS('z','ZZ')
END</lang>
END</syntaxhighlight>


{{out}}
{{out}}
Line 1,755: Line 1,755:


=={{header|SQL}}==
=={{header|SQL}}==
<lang SQL>-- See how many jewels are among the stones
<syntaxhighlight lang="sql">-- See how many jewels are among the stones
declare @S varchar(1024) = 'AaBbCcAa'
declare @S varchar(1024) = 'AaBbCcAa'
, @J varchar(1024) = 'aA';
, @J varchar(1024) = 'aA';
Line 1,788: Line 1,788:
end
end
print 'J='+@J+' S='+@S+' TOTAL = '+cast(@TCNT as varchar(8));
print 'J='+@J+' S='+@S+' TOTAL = '+cast(@TCNT as varchar(8));
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,796: Line 1,796:


=={{header|Swift}}==
=={{header|Swift}}==
<lang swift>func countJewels(_ stones: String, _ jewels: String) -> Int {
<syntaxhighlight lang="swift">func countJewels(_ stones: String, _ jewels: String) -> Int {
return stones.map({ jewels.contains($0) ? 1 : 0 }).reduce(0, +)
return stones.map({ jewels.contains($0) ? 1 : 0 }).reduce(0, +)
}
}


print(countJewels("aAAbbbb", "aA"))
print(countJewels("aAAbbbb", "aA"))
print(countJewels("ZZ", "z"))</lang>
print(countJewels("ZZ", "z"))</syntaxhighlight>


{{out}}
{{out}}
Line 1,809: Line 1,809:


=={{header|Tcl}}==
=={{header|Tcl}}==
<lang Tcl>proc shavej {stones jewels} {
<syntaxhighlight lang="tcl">proc shavej {stones jewels} {
set n 0
set n 0
foreach c [split $stones {}] {
foreach c [split $stones {}] {
Line 1,817: Line 1,817:
}
}
puts [shavej aAAbbbb aA]
puts [shavej aAAbbbb aA]
puts [shavej ZZ z]</lang>
puts [shavej ZZ z]</syntaxhighlight>
{{out}}
{{out}}
3
3
Line 1,823: Line 1,823:


=={{header|Terraform}}==
=={{header|Terraform}}==
<lang hcl>variable "jewels" {
<syntaxhighlight lang="hcl">variable "jewels" {
default = "aA"
default = "aA"
}
}
Line 1,839: Line 1,839:
output "jewel_count" {
output "jewel_count" {
value = length(local.found_jewels)
value = length(local.found_jewels)
}</lang>
}</syntaxhighlight>


{{Out}}
{{Out}}
Line 1,860: Line 1,860:


=={{header|Transd}}==
=={{header|Transd}}==
<lang Scheme>#lang transd
<syntaxhighlight lang="scheme">#lang transd


MainModule: {
MainModule: {
Line 1,870: Line 1,870:
_start: (λ (lout (countJewels "aA" "aAAbbbb"))
_start: (λ (lout (countJewels "aA" "aAAbbbb"))
(lout (countJewels "b" "BB")))
(lout (countJewels "b" "BB")))
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,878: Line 1,878:


=={{header|VBA}}==
=={{header|VBA}}==
{{trans|Phix}}<lang vb>Function count_jewels(stones As String, jewels As String) As Integer
{{trans|Phix}}<syntaxhighlight lang="vb">Function count_jewels(stones As String, jewels As String) As Integer
Dim res As Integer: res = 0
Dim res As Integer: res = 0
For i = 1 To Len(stones)
For i = 1 To Len(stones)
Line 1,888: Line 1,888:
Debug.Print count_jewels("aAAbbbb", "aA")
Debug.Print count_jewels("aAAbbbb", "aA")
Debug.Print count_jewels("ZZ", "z")
Debug.Print count_jewels("ZZ", "z")
End Sub</lang>{{out}}
End Sub</syntaxhighlight>{{out}}
<pre> 3
<pre> 3
0 </pre>
0 </pre>
Line 1,894: Line 1,894:
=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
{{trans|C#}}
{{trans|C#}}
<lang vbnet>Module Module1
<syntaxhighlight lang="vbnet">Module Module1


Function Count(stones As String, jewels As String) As Integer
Function Count(stones As String, jewels As String) As Integer
Line 1,906: Line 1,906:
End Sub
End Sub


End Module</lang>
End Module</syntaxhighlight>
{{out}}
{{out}}
<pre>3
<pre>3
Line 1,913: Line 1,913:
=={{header|Vlang}}==
=={{header|Vlang}}==
{{trans|Go}}
{{trans|Go}}
<lang vlang>fn js(stones string, jewels string) int {
<syntaxhighlight lang="vlang">fn js(stones string, jewels string) int {
mut n := 0
mut n := 0
for b in stones.bytes() {
for b in stones.bytes() {
Line 1,925: Line 1,925:
fn main() {
fn main() {
println(js("aAAbbbb", "aA"))
println(js("aAAbbbb", "aA"))
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,934: Line 1,934:
=={{header|Wren}}==
=={{header|Wren}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang ecmascript>var countJewels = Fn.new { |s, j| s.count { |c| j.contains(c) } }
<syntaxhighlight lang="ecmascript">var countJewels = Fn.new { |s, j| s.count { |c| j.contains(c) } }


System.print(countJewels.call("aAAbbbb", "aA"))
System.print(countJewels.call("aAAbbbb", "aA"))
System.print(countJewels.call("ZZ", "z"))</lang>
System.print(countJewels.call("ZZ", "z"))</syntaxhighlight>


{{out}}
{{out}}
Line 1,946: Line 1,946:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>string 0; \Use zero-terminated strings
<syntaxhighlight lang="xpl0">string 0; \Use zero-terminated strings


func Count(Stones, Jewels);
func Count(Stones, Jewels);
Line 1,968: Line 1,968:
IntOut(0, Count("ZZ", "z")); CrLf(0);
IntOut(0, Count("ZZ", "z")); CrLf(0);
IntOut(0, Count("pack my box with five dozen liquor jugs", "aeiou")); CrLf(0);
IntOut(0, Count("pack my box with five dozen liquor jugs", "aeiou")); CrLf(0);
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 1,978: Line 1,978:


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>fcn countJewels(a,b){ a.inCommon(b).len() }</lang>
<syntaxhighlight lang="zkl">fcn countJewels(a,b){ a.inCommon(b).len() }</syntaxhighlight>
<lang zkl>println(countJewels("aAAbbbb", "aA"));
<syntaxhighlight lang="zkl">println(countJewels("aAAbbbb", "aA"));
println(countJewels("ZZ", "z"));</lang>
println(countJewels("ZZ", "z"));</syntaxhighlight>
{{out}}
{{out}}
<pre>3
<pre>3