Jewels and stones: Difference between revisions
m
syntax highlighting fixup automation
(add bqn) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 29:
{{trans|Python}}
<
R sum(s.map(x -> Int(x C @j)))
print(count_jewels(‘aAAbbbb’, ‘aA’))
print(count_jewels(‘ZZ’, ‘z’))</
{{out}}
Line 43:
=={{header|8080 Assembly}}==
<
jmp demo
;;; Count jewels.
Line 105:
;;; Next free page of memory is used for the jewel array
jpage: equ $/256+1
jarr: equ jpage*256</
{{out}}
Line 114:
=={{header|8086 Assembly}}==
<
bits 16
org 100h
Line 167:
num: db '$'
stones: db 'aAAbbbb',0 ; Example from the task
jewels: db 'aA',0</
{{out}}
Line 174:
=={{header|Ada}}==
<
procedure Jewels_And_Stones is
Line 205:
Show ("aAAbbbb", "aA");
Show ("ZZ", "z");
end Jewels_And_Stones;</
{{out}}
Line 212:
=={{header|ALGOL 68}}==
<
# procedure that counts the number of times the letters in jewels occur in stones #
PROC count jewels = ( STRING stones, jewels )INT:
Line 248:
print( ( count jewels( "ZZ", "z" ), newline ) )
END</
{{out}}
<pre> +3
Line 258:
{{works with|Dyalog APL}}
<syntaxhighlight lang
{{out}}
Line 268:
=={{header|AppleScript}}==
===Functional===
<
on jewelCount(jewels, stones)
set js to chars(jewels)
Line 390:
set my text item delimiters to dlm
str
end unlines</
{{Out}}
<pre>3
Line 397:
===Idiomatic===
<
set counter to 0
considering case
Line 408:
end jewelsAndStones
jewelsAndStones("aAAbBbb", "aAb")</
{{output}}
<syntaxhighlight lang
=={{header|Arturo}}==
<
size select split stones => [in? & jewels]
]
print count "aA" "aAAbbbb"</
{{out}}
Line 425:
=={{header|AutoHotkey}}==
<
for each, jewel in StrSplit(jj)
for each, stone in StrSplit(ss)
Line 431:
num++
return num
}</
Example:<
return</
Outputs:<pre>3</pre>
=={{header|AWK}}==
<
BEGIN {
printf("%d\n",count("aAAbbbb","aA"))
Line 451:
return(total)
}
</syntaxhighlight>
{{out}}
<pre>3
Line 458:
=={{header|BASIC}}==
<
20 FOR A%=1 TO N%
30 READ J$,S$
Line 477:
210 DATA "aA","aAAbbbb"
220 DATA "z","ZZZZ"
</syntaxhighlight>
{{out}}
Line 485:
=={{header|BASIC256}}==
<syntaxhighlight lang="basic256">
function contar_joyas(piedras, joyas)
cont = 0
Line 499:
print contar_joyas("ABCDEFGHIJKLMNOPQRSTUVWXYZ@abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ@abcdefghijklmnopqrstuvwxyz")
print contar_joyas("AB", "")
</syntaxhighlight>
{{out}}
<pre>
Line 506:
=={{header|BCPL}}==
<
let jewels(j, s) = valof
Line 525:
$( show("aA", "aAAbbbb")
show("z", "ZZ")
$)</
{{out}}
<pre>"aA" in "aAAbbbb": 3
Line 533:
Similar in nature to APL, mostly due to how trivial the problem is in an array language.
<syntaxhighlight lang
<syntaxhighlight lang="text"> "aA" Jewels "aAAbbbb"
3</
=={{header|Bracmat}}==
<
= stones jewels N
. !arg:(?stones.?jewels)
Line 556:
)
& f$(aAAbbbb.aA)
</syntaxhighlight>
'''Output'''
<pre>3</pre>
Line 562:
=={{header|C}}==
{{trans|Kotlin}}
<
#include <string.h>
Line 575:
printf("%d\n", count_jewels("ZZ", "z"));
return 0;
}</
{{output}}
Line 584:
=={{header|C sharp}}==
<
using System.Linq;
Line 598:
return stones.Count(bag.Contains);
}
}</
{{out}}
<pre>
Line 607:
=={{header|C++}}==
{{trans|D}}
<
#include <iostream>
Line 627:
return 0;
}</
{{out}}
<pre>3
Line 633:
=={{header|CLU}}==
<
is_jewel: array[bool] := array[bool]$fill(0, 256, false)
for c: char in string$chars(jewels) do
Line 655:
start_up = proc ()
show("aA", "aAAbbbb")
end start_up</
{{out}}
<pre>"aA" in "aAAbbbb": 3</pre>
=={{header|Cowgol}}==
<
sub count_jewels(stones: [uint8], jewels: [uint8]): (count: uint16) is
Line 688:
print_and_count("aAAbbbb", "aA");
print_and_count("ZZ", "z");</
{{out}}
Line 696:
=={{header|Crystal}}==
<
stones.count(jewels) # => 3
Line 705:
# This works as intended though:
stones.count { |c| jewels.chars.includes?(c) } # => 2</
=={{header|D}}==
{{trans|Kotlin}}
<
import std.stdio;
Line 725:
countJewels("aAAbbbb", "aA").writeln;
countJewels("ZZ", "z").writeln;
}</
{{out}}
<pre>3
Line 731:
=={{header|Draco}}==
<
[256] bool jewel;
word count;
Line 758:
show("aA", "aAAbbbb");
show("z", "ZZ")
corp</
{{out}}
<pre>'aA' in 'aAAbbbb': 3
Line 767:
{{trans|Swift}}
<
stones.Iterate().Map(x => jewels.Contains(x) ? 1 : 0).Reduce((x,y) => x + y, 0)
}
print(countJewels("aAAbbbb", "aA"))
print(countJewels("ZZ", "z"))</
{{out}}
Line 780:
=={{header|F_Sharp|F#}}==
<
let fN jewels stones=stones|>Seq.filter(fun n->Seq.contains n jewels)|>Seq.length
printfn "%d" (fN "aA" "aAAbbbb")
</syntaxhighlight>
{{out}}
<pre>
Line 790:
=={{header|Factor}}==
<
: count-jewels ( stones jewels -- n ) [ member? ] curry count ;
"aAAbbbb" "aA"
"ZZ" "z" [ count-jewels . ] 2bi@</
{{out}}
<pre>
Line 803:
=={{header|Euphoria}}==
<syntaxhighlight lang="euphoria">
function number_of(object jewels, object stones) -- why limit ourselves to strings?
integer ct = 0
Line 815:
? 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!
</syntaxhighlight>
{{out}}
<pre>
Line 824:
=={{header|FreeBASIC}}==
<
function contar_joyas(piedras as string, joyas as string) as integer
dim as integer bc, cont: cont = 0
Line 839:
"ABCDEFGHIJKLMNOPQRSTUVWXYZ@abcdefghijklmnopqrstuvwxyz")
print contar_joyas("AB", "")
</syntaxhighlight>
{{out}}
<pre>
Line 854:
'''Outer loop stones, index into jewels:'''
<
import (
Line 872:
func main() {
fmt.Println(js("aAAbbbb", "aA"))
}</
{{out}}
<pre>3</pre>
'''Outer loop jewels, count stones:'''
<
for _, b := range []byte(jewels) {
n += strings.Count(stones, string(b))
}
return
}</
'''Construct jewel set, then loop over stones:'''
<
var jSet ['z' + 1]int
for _, b := range []byte(jewels) {
Line 894:
}
return
}</
'''Construct stone multiset, then loop over jewels:'''
<
var sset ['z' + 1]int
for _, b := range []byte(stones) {
Line 906:
}
return
}</
=={{header|Haskell}}==
<
:: Eq a
=> [a] -> [a] -> Int
Line 921:
main :: IO ()
main = mapM_ print $ uncurry jewelCount <$> [("aA", "aAAbbbb"), ("z", "ZZ")]
</syntaxhighlight>
{{Out}}
<pre>3
Line 928:
Or in terms of filter rather than foldr
<
:: Eq a
=> [a] -> [a] -> Int
Line 937:
main = do
print $ jewelCount "aA" "aAAbbbb"
print $ jewelCount "z" "ZZ"</
{{Out}}
<pre>3
Line 943:
=={{header|J}}==
<syntaxhighlight lang="j">
NB. jewels sums a raveled equality table
NB. use: x jewels y x are the stones, y are the jewels.
Line 957:
0
</syntaxhighlight>
=={{header|Java}}==
<
import java.util.Set;
Line 984:
System.out.println(countJewels("ZZ", "z"));
}
}</
{{out}}
<pre>3
Line 990:
=={{header|Javascript}}==
<
// jewelCount :: String -> String -> Int
Line 1,005:
]
.map(x => jewelCount(...x))
})();</
{{Out}}
<pre>[3, 0]</pre>
=={{header|jq}}==
<
[$stones|split("") as $s|$jewels|split("") as $j|$s[]|
select(. as $c|$j|contains([$c]))]|length'</
{{Out}}
Line 1,020:
'''Module''':
<
count(s, j) = Base.count(x ∈ j for x in s)
end # module Jewels</
'''Main''':
<
@show Jewels.count("ZZ", "z")</
{{out}}
Line 1,035:
=={{header|Kotlin}}==
<
fun countJewels(s: String, j: String) = s.count { it in j }
Line 1,042:
println(countJewels("aAAbbbb", "aA"))
println(countJewels("ZZ", "z"))
}</
{{output}}
Line 1,051:
=={{header|Lambdatalk}}==
<
{def countjewels
{def countjewels.r
Line 1,068:
{countjewels aAAbbbb aA} -> 3
{countjewels ZZ z} -> 0
</syntaxhighlight>
=={{header|Lua}}==
{{trans|C}}
<
local count = 0
for i=1,#s do
Line 1,084:
print(count_jewels("aAAbbbb", "aA"))
print(count_jewels("ZZ", "z"))</
{{out}}
<pre>3
Line 1,090:
=={{header|Maple}}==
<
local count, j, letter:
j := convert(jewels,set):
Line 1,101:
return count:
end proc:
count_jewel("aAAbbbb", "aA")</
{{Out|Output}}
<pre>3</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
JewelsStones["aA", "aAAbbbb"]
JewelsStones["ZZ", "z"]</
{{out}}
<pre>3
Line 1,114:
=={{header|MATLAB}} / {{header|Octave}}==
<syntaxhighlight lang="matlab">
function s = count_jewels(stones,jewels)
s=0;
Line 1,124:
%!test
%! assert(count_jewels('ZZ','z'),0)
</syntaxhighlight>
=={{header|min}}==
{{works with|min|0.19.6}}
<
((1 0 if?) concat map sum) :count
Line 1,137:
"aAAbbbb" "aA" count-jewels puts!
"ZZ" "z" count-jewels puts!</
{{out}}
<pre>
Line 1,145:
=={{header|Modula-2}}==
<
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 1,180:
ReadChar
END Jewels.</
{{out}}
<pre>3
Line 1,186:
=={{header|Nim}}==
<
func countJewels(stones, jewels: string): Natural =
Line 1,192:
echo countJewels("aAAbbbb", "aA")
echo countJewels("ZZ", "z")</
{{out}}
Line 1,200:
=={{header|Objeck}}==
{{trans|Java}}
<
class JewelsStones {
Line 1,224:
return count;
}
}</
{{out}}
Line 1,233:
=={{header|Perl}}==
<
my( $j, $s ) = @_;
my($c,%S);
Line 1,243:
print count_jewels 'aA' , 'aAAbbbb';
print count_jewels 'z' , 'ZZ';</
{{out}}
<pre>3
0</pre>
===Alternate using regex===
<
use strict; # https://rosettacode.org/wiki/Jewels_and_Stones#Perl
Line 1,259:
aAAbbbb abc
ZZ z
END</
{{out}}
<pre>
Line 1,268:
=={{header|Phix}}==
<!--<
<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>
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;">"ZZ"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"z"</span><span style="color: #0000FF;">)</span>
<!--</
{{out}}
<pre>
Line 1,288:
===List comprehension===
<
===Recursion===
<
jewels_and_stones2(Jewels,Stones,0,N).
Line 1,307:
N1 = N0
),
jewels_and_stones2_(J,Stones,N1,N).</
===Foreach loop===
<
N = 0,
foreach(J in Jewels, S in Stones)
Line 1,316:
N := N + 1
end
end.</
===Test===
<
Tests = [["aA","aAAbbbb"],
["z","ZZ"]
Line 1,331:
nl
end,
nl.</
{{out}}
Line 1,347:
===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).
<
Alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
Len = Alpha.len,
Line 1,361:
time(println(js2=jewels_and_stones2(Jewels,Stones))),
time(println(js3=jewels_and_stones3(Jewels,Stones))),
nl.</
{{out}}
Line 1,383:
=={{header|PL/M}}==
<
/* FIND JEWELS AMONG STONES */
Line 1,458:
CALL BDOS(0,0);
EOF</
{{out}}
<pre>'aA' IN 'aAAbbbb': 3
Line 1,465:
=={{header|Prolog}}==
<
:- system:set_prolog_flag(double_quotes,codes) .
Line 1,475:
.
</syntaxhighlight>
{{out}}
Line 1,496:
{{works with|SWI Prolog}}
<
string_codes(Stones, Scodes),
string_codes(Jewels, Jcodes),
Line 1,514:
count_jewels([S|Stones], Jewels, N, R).
count_jewels([_|Stones], Jewels, N, R):-
count_jewels(Stones, Jewels, N, R).</
{{out}}
Line 1,530:
=={{header|Python}}==
<
return sum(x in j for x in s)
print countJewels("aAAbbbb", "aA")
print countJewels("ZZ", "z")</
{{Out}}
<pre>3
Line 1,540:
===Python 3 Alternative===
<
jewelset = set(jewels)
return sum(1 for stone in stones if stone in jewelset)
print(countJewels("aAAbbbb", "aA"))
print(countJewels("ZZ", "z"))</
{{Out}}
<pre>3
Line 1,551:
=={{header|R}}==
<
stones <- unlist(strsplit(stones, split = "")) # obtain a character vector
jewels <- unlist(strsplit(jewels, split = ""))
Line 1,560:
print(J_n_S("ZZ", "z"))
print(J_n_S("lgGKJGljglghGLGHlhglghoIPOgfdtrdDCHnvbnmBVC", "fFgGhH"))
</syntaxhighlight>
{{Out}}
<pre>> print(J_n_S("aAAbbbb", "aA"))
Line 1,572:
=={{header|Quackery}}==
<
witheach [ bit | ]
rot witheach
Line 1,579:
drop ] is j&s ( $ $ --> n )
$ "aAAbbbb" $ "aA" j&s echo</
{{out}}
Line 1,586:
=={{header|Racket}}==
<
(define (jewels-and-stones stones jewels)
Line 1,594:
(jewels-and-stones "aAAbbbb" "aA")
(jewels-and-stones "ZZ" "z"))
</syntaxhighlight>
{{out}}
<pre>3
Line 1,601:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
my %counts_of_all = $s.comb.Bag;
my @jewel_list = $j.comb.unique;
Line 1,609:
say count-jewels 'aA' , 'aAAbbbb';
say count-jewels 'z' , 'ZZ';</
{{Out}}
<pre>3
Line 1,615:
=={{header|Red}}==
<
title: "Jewels and stones"
red-version: 0.6.4
Line 1,642:
print count-jewels "aAAbbbb" "aA"
print count-jewels "ZZ" "z"</
{{out}}
<pre>
Line 1,651:
=={{header|REXX}}==
Programming note: a check is made so that only (Latin) letters are counted as a match.
<
say count('aAAbbbb', "aA")
say count('ZZ' , "z" )
Line 1,662:
if datatype(x, 'M') then if pos(x, jewels)\==0 then #= # + 1
end /*j*/ /* [↑] if a letter and a match, bump #*/
return # /*return the number of common letters. */</
{{out|output|text= when using the default inputs:}}
<pre>3
Line 1,668:
=={{header|Ring}}==
<
jewels = "aA"
Line 1,686:
next
return num
</syntaxhighlight>
Output:
<pre>3
Line 1,692:
=={{header|Ruby}}==
<
stones.count(jewels) # => 3
</syntaxhighlight>
=={{header|Rust}}==
<
let mut count: u8 = 0;
for cur_char in stones.chars() {
Line 1,711:
println!("{}", count_jewels("ZZ", "z"));
}
</syntaxhighlight>
Output:<pre>3
0</pre>
=={{header|Scala}}==
<
def countJewels(s: String, j: String): Int = s.count(i => j.contains(i))
println(countJewels("aAAbbbb", "aA"))
println(countJewels("ZZ", "z"))
}</
{{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}}==
<
s.chars.count { |c|
j.contains(c)
Line 1,732:
say countJewels("aAAbbbb", "aA") #=> 3
say countJewels("ZZ", "z") #=> 0</
=={{header|Snobol}}==
<
DEFINE('JEWELS(JWL,STN)') :(JEWELS_END)
JEWELS JEWELS = 0
Line 1,747:
* Example with no jewels (prints 0)
OUTPUT = JEWELS('z','ZZ')
END</
{{out}}
Line 1,755:
=={{header|SQL}}==
<
declare @S varchar(1024) = 'AaBbCcAa'
, @J varchar(1024) = 'aA';
Line 1,788:
end
print 'J='+@J+' S='+@S+' TOTAL = '+cast(@TCNT as varchar(8));
</syntaxhighlight>
{{out}}
Line 1,796:
=={{header|Swift}}==
<
return stones.map({ jewels.contains($0) ? 1 : 0 }).reduce(0, +)
}
print(countJewels("aAAbbbb", "aA"))
print(countJewels("ZZ", "z"))</
{{out}}
Line 1,809:
=={{header|Tcl}}==
<
set n 0
foreach c [split $stones {}] {
Line 1,817:
}
puts [shavej aAAbbbb aA]
puts [shavej ZZ z]</
{{out}}
3
Line 1,823:
=={{header|Terraform}}==
<
default = "aA"
}
Line 1,839:
output "jewel_count" {
value = length(local.found_jewels)
}</
{{Out}}
Line 1,860:
=={{header|Transd}}==
<
MainModule: {
Line 1,870:
_start: (λ (lout (countJewels "aA" "aAAbbbb"))
(lout (countJewels "b" "BB")))
}</
{{out}}
<pre>
Line 1,878:
=={{header|VBA}}==
{{trans|Phix}}<
Dim res As Integer: res = 0
For i = 1 To Len(stones)
Line 1,888:
Debug.Print count_jewels("aAAbbbb", "aA")
Debug.Print count_jewels("ZZ", "z")
End Sub</
<pre> 3
0 </pre>
Line 1,894:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Function Count(stones As String, jewels As String) As Integer
Line 1,906:
End Sub
End Module</
{{out}}
<pre>3
Line 1,913:
=={{header|Vlang}}==
{{trans|Go}}
<
mut n := 0
for b in stones.bytes() {
Line 1,925:
fn main() {
println(js("aAAbbbb", "aA"))
}</
{{out}}
Line 1,934:
=={{header|Wren}}==
{{trans|Kotlin}}
<
System.print(countJewels.call("aAAbbbb", "aA"))
System.print(countJewels.call("ZZ", "z"))</
{{out}}
Line 1,946:
=={{header|XPL0}}==
<
func Count(Stones, Jewels);
Line 1,968:
IntOut(0, Count("ZZ", "z")); CrLf(0);
IntOut(0, Count("pack my box with five dozen liquor jugs", "aeiou")); CrLf(0);
]</
{{out}}
Line 1,978:
=={{header|zkl}}==
<
<
println(countJewels("ZZ", "z"));</
{{out}}
<pre>3
|