Jewels and stones: Difference between revisions
Content added Content deleted
(add bqn) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 29: | Line 29: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<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’))</ |
print(count_jewels(‘ZZ’, ‘z’))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 43: | Line 43: | ||
=={{header|8080 Assembly}}== |
=={{header|8080 Assembly}}== |
||
< |
<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</ |
jarr: equ jpage*256</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 114: | Line 114: | ||
=={{header|8086 Assembly}}== |
=={{header|8086 Assembly}}== |
||
< |
<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</ |
jewels: db 'aA',0</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 174: | Line 174: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<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;</ |
end Jewels_And_Stones;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 212: | Line 212: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
< |
<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</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> +3 |
<pre> +3 |
||
Line 258: | Line 258: | ||
{{works with|Dyalog APL}} |
{{works with|Dyalog APL}} |
||
<lang |
<syntaxhighlight lang="apl">jewels ← +/∊⍨</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 268: | Line 268: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
===Functional=== |
===Functional=== |
||
< |
<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</ |
end unlines</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>3 |
<pre>3 |
||
Line 397: | Line 397: | ||
===Idiomatic=== |
===Idiomatic=== |
||
< |
<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")</ |
jewelsAndStones("aAAbBbb", "aAb")</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
<lang |
<syntaxhighlight lang="applescript">6</syntaxhighlight> |
||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">count: function [jewels,stones][ |
||
size select split stones => [in? & jewels] |
size select split stones => [in? & jewels] |
||
] |
] |
||
print count "aA" "aAAbbbb"</ |
print count "aA" "aAAbbbb"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 425: | Line 425: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<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 |
||
}</ |
}</syntaxhighlight> |
||
Example:< |
Example:<syntaxhighlight lang="autohotkey">MsgBox % JewelsandStones("aAAbbbbz", "aAZ") |
||
return</ |
return</syntaxhighlight> |
||
Outputs:<pre>3</pre> |
Outputs:<pre>3</pre> |
||
=={{header|AWK}}== |
=={{header|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}}== |
||
< |
<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}}== |
||
< |
<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") |
||
$)</ |
$)</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 |
<syntaxhighlight lang="bqn">Jewels←+´∊˜</syntaxhighlight> |
||
<lang> "aA" Jewels "aAAbbbb" |
<syntaxhighlight lang="text"> "aA" Jewels "aAAbbbb" |
||
3</ |
3</syntaxhighlight> |
||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
< |
<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}} |
||
< |
<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; |
||
}</ |
}</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
Line 584: | Line 584: | ||
=={{header|C sharp}}== |
=={{header|C sharp}}== |
||
< |
<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); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 607: | Line 607: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="cpp">#include <algorithm> |
||
#include <iostream> |
#include <iostream> |
||
Line 627: | Line 627: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>3 |
<pre>3 |
||
Line 633: | Line 633: | ||
=={{header|CLU}}== |
=={{header|CLU}}== |
||
< |
<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</ |
end start_up</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>"aA" in "aAAbbbb": 3</pre> |
<pre>"aA" in "aAAbbbb": 3</pre> |
||
=={{header|Cowgol}}== |
=={{header|Cowgol}}== |
||
< |
<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");</ |
print_and_count("ZZ", "z");</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 696: | Line 696: | ||
=={{header|Crystal}}== |
=={{header|Crystal}}== |
||
< |
<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</ |
stones.count { |c| jewels.chars.includes?(c) } # => 2</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<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; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>3 |
<pre>3 |
||
Line 731: | Line 731: | ||
=={{header|Draco}}== |
=={{header|Draco}}== |
||
< |
<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</ |
corp</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>'aA' in 'aAAbbbb': 3 |
<pre>'aA' in 'aAAbbbb': 3 |
||
Line 767: | Line 767: | ||
{{trans|Swift}} |
{{trans|Swift}} |
||
< |
<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"))</ |
print(countJewels("ZZ", "z"))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 780: | Line 780: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<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}}== |
||
< |
<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@</ |
"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}}== |
||
< |
<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:''' |
||
< |
<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")) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>3</pre> |
<pre>3</pre> |
||
'''Outer loop jewels, count stones:''' |
'''Outer loop jewels, count stones:''' |
||
< |
<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 |
||
}</ |
}</syntaxhighlight> |
||
'''Construct jewel set, then loop over stones:''' |
'''Construct jewel set, then loop over stones:''' |
||
< |
<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 |
||
}</ |
}</syntaxhighlight> |
||
'''Construct stone multiset, then loop over jewels:''' |
'''Construct stone multiset, then loop over jewels:''' |
||
< |
<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 |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<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 |
||
< |
<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"</ |
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}}== |
||
< |
<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")); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>3 |
<pre>3 |
||
Line 990: | Line 990: | ||
=={{header|Javascript}}== |
=={{header|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)) |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>[3, 0]</pre> |
<pre>[3, 0]</pre> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
< |
<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'</ |
select(. as $c|$j|contains([$c]))]|length'</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 1,020: | Line 1,020: | ||
'''Module''': |
'''Module''': |
||
< |
<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</ |
end # module Jewels</syntaxhighlight> |
||
'''Main''': |
'''Main''': |
||
< |
<syntaxhighlight lang="julia">@show Jewels.count("aAAbbbb", "aA") |
||
@show Jewels.count("ZZ", "z")</ |
@show Jewels.count("ZZ", "z")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,035: | Line 1,035: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<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")) |
||
}</ |
}</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
Line 1,051: | Line 1,051: | ||
=={{header|Lambdatalk}}== |
=={{header|Lambdatalk}}== |
||
< |
<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}} |
||
< |
<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"))</ |
print(count_jewels("ZZ", "z"))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>3 |
<pre>3 |
||
Line 1,090: | Line 1,090: | ||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<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")</ |
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}}== |
||
< |
<syntaxhighlight lang="mathematica">JewelsStones[j_String, s_String] := Count[MemberQ[Characters[j], #] & /@ Characters[s], True] |
||
JewelsStones["aA", "aAAbbbb"] |
JewelsStones["aA", "aAAbbbb"] |
||
JewelsStones["ZZ", "z"]</ |
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}} |
||
< |
<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!</ |
"ZZ" "z" count-jewels puts!</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,145: | Line 1,145: | ||
=={{header|Modula-2}}== |
=={{header|Modula-2}}== |
||
< |
<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.</ |
END Jewels.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>3 |
<pre>3 |
||
Line 1,186: | Line 1,186: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<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")</ |
echo countJewels("ZZ", "z")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,200: | Line 1,200: | ||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="objeck">use Collection.Generic; |
||
class JewelsStones { |
class JewelsStones { |
||
Line 1,224: | Line 1,224: | ||
return count; |
return count; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,233: | Line 1,233: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<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';</ |
print count_jewels 'z' , 'ZZ';</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>3 |
<pre>3 |
||
0</pre> |
0</pre> |
||
===Alternate using regex=== |
===Alternate using regex=== |
||
< |
<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</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,268: | Line 1,268: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<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> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,288: | Line 1,288: | ||
===List comprehension=== |
===List comprehension=== |
||
< |
<syntaxhighlight lang="picat">jewels_and_stones1(Jewels,Stones) = sum([1 : S in Stones, J in Jewels, S == J]).</syntaxhighlight> |
||
===Recursion=== |
===Recursion=== |
||
< |
<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).</ |
jewels_and_stones2_(J,Stones,N1,N).</syntaxhighlight> |
||
===Foreach loop=== |
===Foreach loop=== |
||
< |
<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.</ |
end.</syntaxhighlight> |
||
===Test=== |
===Test=== |
||
< |
<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.</ |
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). |
||
< |
<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.</ |
nl.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,383: | Line 1,383: | ||
=={{header|PL/M}}== |
=={{header|PL/M}}== |
||
< |
<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</ |
EOF</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>'aA' IN 'aAAbbbb': 3 |
<pre>'aA' IN 'aAAbbbb': 3 |
||
Line 1,465: | Line 1,465: | ||
=={{header|Prolog}}== |
=={{header|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}} |
||
< |
<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).</ |
count_jewels(Stones, Jewels, N, R).</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,530: | Line 1,530: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<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")</ |
print countJewels("ZZ", "z")</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>3 |
<pre>3 |
||
Line 1,540: | Line 1,540: | ||
===Python 3 Alternative=== |
===Python 3 Alternative=== |
||
< |
<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"))</ |
print(countJewels("ZZ", "z"))</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>3 |
<pre>3 |
||
Line 1,551: | Line 1,551: | ||
=={{header|R}}== |
=={{header|R}}== |
||
< |
<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}}== |
||
< |
<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</ |
$ "aAAbbbb" $ "aA" j&s echo</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,586: | Line 1,586: | ||
=={{header|Racket}}== |
=={{header|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 |
<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';</ |
say count-jewels 'z' , 'ZZ';</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>3 |
<pre>3 |
||
Line 1,615: | Line 1,615: | ||
=={{header|Red}}== |
=={{header|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"</ |
print count-jewels "ZZ" "z"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,651: | Line 1,651: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Programming note: a check is made so that only (Latin) letters are counted as a match. |
Programming note: a check is made so that only (Latin) letters are counted as a match. |
||
< |
<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. */</ |
return # /*return the number of common letters. */</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre>3 |
<pre>3 |
||
Line 1,668: | Line 1,668: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<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}}== |
||
< |
<syntaxhighlight lang="ruby">stones, jewels = "aAAbbbb", "aA" |
||
stones.count(jewels) # => 3 |
stones.count(jewels) # => 3 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<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}}== |
||
< |
<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")) |
||
}</ |
}</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}}== |
||
< |
<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</ |
say countJewels("ZZ", "z") #=> 0</syntaxhighlight> |
||
=={{header|Snobol}}== |
=={{header|Snobol}}== |
||
< |
<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</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,755: | Line 1,755: | ||
=={{header|SQL}}== |
=={{header|SQL}}== |
||
< |
<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}}== |
||
< |
<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"))</ |
print(countJewels("ZZ", "z"))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,809: | Line 1,809: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<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]</ |
puts [shavej ZZ z]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
3 |
3 |
||
Line 1,823: | Line 1,823: | ||
=={{header|Terraform}}== |
=={{header|Terraform}}== |
||
< |
<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) |
||
}</ |
}</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 1,860: | Line 1,860: | ||
=={{header|Transd}}== |
=={{header|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"))) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,878: | Line 1,878: | ||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
{{trans|Phix}}< |
{{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</ |
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#}} |
||
< |
<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</ |
End Module</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>3 |
<pre>3 |
||
Line 1,913: | Line 1,913: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<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")) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,934: | Line 1,934: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<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"))</ |
System.print(countJewels.call("ZZ", "z"))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,946: | Line 1,946: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<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); |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,978: | Line 1,978: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">fcn countJewels(a,b){ a.inCommon(b).len() }</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">println(countJewels("aAAbbbb", "aA")); |
||
println(countJewels("ZZ", "z"));</ |
println(countJewels("ZZ", "z"));</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>3 |
<pre>3 |