Jewels and stones: Difference between revisions

add ABC
No edit summary
(add ABC)
 
(34 intermediate revisions by 22 users not shown)
Line 29:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F count_jewels(s, j)
R sum(s.map(x -> Int(x C @j)))
 
print(count_jewels(‘aAAbbbb’, ‘aA’))
print(count_jewels(‘ZZ’, ‘z’))</langsyntaxhighlight>
 
{{out}}
Line 41:
</pre>
 
=={{header|8080360 Assembly}}==
<syntaxhighlight lang="360asm">* Jewels and stones - 24/04/2023
JEWELS CSECT
USING JEWELS,R13 base register
B 72(R15) skip savearea
DC 17F'0' savearea
SAVE (14,12) save previous context
ST R13,4(R15) link backward
ST R15,8(R13) link forward
LR R13,R15 set addressability
LA R7,EX1 @ex(1,1)
LA R6,1 i=1
DO WHILE=(CH,R6,LE,N) do i=1 to n
MVC CSTO,0(R7) csto=ex(i,1)
MVC CJEW,16(R7) cjew=ex(i,2)
BAL R14,COUNT r0=count(csto,cjew)
LR R1,R0 count
XDECO R1,XDEC edit count
MVC PG(6),XDEC+6 output count
XPRNT PG,L'PG print buffer
LA R7,32(R7) @ex+=32
LA R6,1(R6) i++
ENDDO , enddo i
FIN L R13,4(0,R13) restore previous savearea pointer
RETURN (14,12),RC=0 restore registers from calling save
*
COUNT CNOP 0,4 count(csto,cjew) -------------------
STM R2,R14,COUNTSA save context
MVC CC,CSTO cc=csto
BAL R14,LENGTH call length(csto)
STH R0,LCSTO lcsto=length(csto)
MVC CC,CJEW cc=cjew
BAL R14,LENGTH call length(cjew)
STH R0,LCJEW lcjew=length(cjew)
XR R3,R3 ret=0
LA R6,1 i=1
DO WHILE=(CH,R6,LE,LCSTO) do i=1 to length(csto)
LA R2,CSTO-1 @csto-1
AR R2,R6 @csto-1+i
MVC C,0(R2) c=substr(csto,i,1)
SR R1,R1 k=0 ....index(cjew,c)...........
LA R7,1 j=1 |
DO WHILE=(CH,R7,LE,LCJEW) do j=1 to length(cjew) |
LA R9,CJEW-1 @cjew-1 |
AR R9,R7 @cjew-1+j |
IF CLC,0(1,R9),EQ,C THEN if substr(cjew,j,1)=c then |
LR R1,R7 k=j |
ENDIF , endif |
LA R7,1(R7) j++ |
ENDDO , enddo j ........................
IF LTR,R1,NZ,R1 THEN if index(cjew,c)<>0 then
LA R3,1(R3) ret=ret+1
ENDIF , endif
LA R6,1(R6) i++
ENDDO , enddo i
LR R0,R3 return value ret
LM R2,R14,COUNTSA restore context
BR R14 return
COUNTSA DS 14A context store -- end count ---------
*
LENGTH CNOP 0,4 length(cc) -------------------------
STM R2,R14,LENGTSA save context
LA R10,0 l=0
LA R9,CC @cc
LA R8,1 k=1
DO WHILE=(C,R8,LE,=A(16)) do k=1 to 16
MVC C,0(R9) c=subsctr(cc,k,1)
IF CLC,C,NE,=C' ' THEN if c<>' ' then
LA R10,1(R10) l=l+1
ELSE , else
B LEAVEK leave k
ENDIF , endif
LA R9,1(R9) @cc++
LA R8,1(R8) k++
ENDDO , enddo k
LEAVEK LR R0,R10 set return value
LM R2,R14,LENGTSA restore context
BR R14 return
LENGTSA DS 14A context store -- end length --------
*
N DC H'2' n
EX1 DC CL16'aAAbbbb' stones(1)
DC CL16'aA' jewels(1)
EX2 DC CL16'ZZ' stones(2)
DC CL16'z' jewels(2)
CSTO DS CL16 csto
CJEW DS CL16 cjew
LCSTO DS H length of csto
LCJEW DS H length of cjew
CC DS CL16 cc
C DS CL1 c
PG DC CL80' ' buffer
XDEC DS CL12 temp for xdeco
REGEQU
END JEWELS</syntaxhighlight>
{{out}}
<pre>
3
0
</pre>
 
=={{header|8080 Assembly}}==
<lang 8080asm> org 100h
<syntaxhighlight lang="8080asm"> org 100h
jmp demo
;;; Count jewels.
Line 105 ⟶ 205:
;;; Next free page of memory is used for the jewel array
jpage: equ $/256+1
jarr: equ jpage*256</langsyntaxhighlight>
 
{{out}}
 
<pre>3</pre>
 
 
=={{header|8086 Assembly}}==
 
<langsyntaxhighlight lang="asm"> cpu 8086
bits 16
org 100h
Line 167 ⟶ 266:
num: db '$'
stones: db 'aAAbbbb',0 ; Example from the task
jewels: db 'aA',0</langsyntaxhighlight>
 
{{out}}
 
<pre>3</pre>
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program jewels64.s */
 
/************************************/
/* Constantes */
/************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
 
/************************************/
/* Initialized data */
/************************************/
.data
szMessResult: .asciz "Result: "
szStone1: .asciz "aAAbbbb"
szJewels1: .asciz "aA"
szStone2: .asciz "ZZ"
szJewels2: .asciz "z"
szCarriageReturn: .asciz "\n"
szMessStart: .asciz "Program 64 bits start.\n"
/************************************/
/* UnInitialized data */
/************************************/
.bss
sZoneConv: .skip 24
/************************************/
/* code section */
/************************************/
.text
.global main
main: // entry of program
ldr x0,qAdrszMessStart
bl affichageMess
 
ldr x0,qAdrszStone1
ldr x1,qAdrszJewels1
bl countJewels
ldr x0,qAdrszStone2
ldr x1,qAdrszJewels2
bl countJewels
 
100: // standard end of the program
mov x0, #0 // return code
mov x8, #EXIT // request to exit program
svc 0 // perform the system call
qAdrszStone1: .quad szStone1
qAdrszJewels1: .quad szJewels1
qAdrszStone2: .quad szStone2
qAdrszJewels2: .quad szJewels2
qAdrsZoneConv: .quad sZoneConv
qAdrszMessResult: .quad szMessResult
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrszMessStart: .quad szMessStart
/***************************************************/
/* count jewels in stone */
/***************************************************/
/* r0 contains stone address */
/* r1 contains jewels address */
/* r0 return jewels count */
countJewels:
stp x1,lr,[sp,-16]!
stp x2,x3,[sp,-16]!
stp x4,x5,[sp,-16]!
stp x6,x7,[sp,-16]!
mov x4,#0 // counter
mov x3,#0 // index stone
1:
ldrb w6,[x0,x3] // load byte of stone
cmp x6,#0 // end stone ?
beq 3f
mov x5,#0 // index jewels
2:
ldrb w2,[x1,x5] // load byte of jewels
cmp x2,#0 // end jewels ?
cinc x3,x3,eq // yes -> increment index stone
beq 1b // and loop
cmp x6,x2 // compare byte
cinc x5,x5,ne // not equal -> increment jewels index
bne 2b // and loop
add x4,x4,#1 // else increment counter
add x3,x3,#1 // incremente index stone
b 1b // and loop
 
3: // result display
mov x0,x4
ldr x1,qAdrsZoneConv
bl conversion10
ldr x0,qAdrszMessResult
bl affichageMess
ldr x0,qAdrsZoneConv
bl affichageMess
ldr x0,qAdrszCarriageReturn
bl affichageMess
mov x0,x4
100:
ldp x6,x7,[sp],16
ldp x4,x5,[sp],16
ldp x2,x3,[sp],16
ldp x1,lr,[sp],16
ret
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeARM64.inc"
 
</syntaxhighlight>
{{Out}}
<pre>
Program 64 bits start.
Result: 3
Result: 0
</pre>
=={{header|ABC}}==
<syntaxhighlight lang="abc">HOW TO RETURN stones count.in jewels:
PUT 0 IN count
FOR stone IN stones:
IF stone in jewels: PUT count+1 IN count
RETURN count
 
WRITE "aAAbbbb" count.in "aA"/
WRITE "ZZ" count.in "z"/</syntaxhighlight>
{{out}}
<pre>3
0</pre>
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
 
procedure Jewels_And_Stones is
Line 205 ⟶ 433:
Show ("aAAbbbb", "aA");
Show ("ZZ", "z");
end Jewels_And_Stones;</langsyntaxhighlight>
 
{{out}}
Line 212 ⟶ 440:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">BEGIN
# procedure that counts the number of times the letters in jewels occur in stones #
PROC count jewels = ( STRING stones, jewels )INT:
Line 248 ⟶ 476:
print( ( count jewels( "ZZ", "z" ), newline ) )
 
END</langsyntaxhighlight>
{{out}}
<pre> +3
Line 258 ⟶ 486:
{{works with|Dyalog APL}}
 
<syntaxhighlight lang ="apl">jewels ← +/∊⍨</langsyntaxhighlight>
 
{{out}}
Line 268 ⟶ 496:
=={{header|AppleScript}}==
===Functional===
<langsyntaxhighlight lang="applescript">-- jewelCount :: String -> String -> Int
on jewelCount(jewels, stones)
set js to chars(jewels)
Line 390 ⟶ 618:
set my text item delimiters to dlm
str
end unlines</langsyntaxhighlight>
{{Out}}
<pre>3
Line 397 ⟶ 625:
===Idiomatic===
 
<langsyntaxhighlight lang="applescript">on jewelsAndStones(stones, jewels)
set counter to 0
considering case
Line 408 ⟶ 636:
end jewelsAndStones
 
jewelsAndStones("aAAbBbb", "aAb")</langsyntaxhighlight>
 
{{output}}
<syntaxhighlight lang ="applescript">6</langsyntaxhighlight>
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program jewels.s */
 
/************************************/
/* Constantes */
/************************************/
/* for this file see task include a file in language ARM assembly*/
.include "../constantes.inc"
 
/************************************/
/* Initialized data */
/************************************/
.data
szMessResult: .asciz "Result: "
szStone1: .asciz "aAAbbbb"
szJewels1: .asciz "aA"
szStone2: .asciz "ZZ"
szJewels2: .asciz "z"
szCarriageReturn: .asciz "\n"
szMessStart: .asciz "Program 32 bits start.\n"
/************************************/
/* UnInitialized data */
/************************************/
.bss
sZoneConv: .skip 24
/************************************/
/* code section */
/************************************/
.text
.global main
main: @ entry of program
ldr r0,iAdrszMessStart
bl affichageMess
 
ldr r0,iAdrszStone1
ldr r1,iAdrszJewels1
bl countJewels
ldr r0,iAdrszStone2
ldr r1,iAdrszJewels2
bl countJewels
 
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc 0 @ perform the system call
iAdrszStone1: .int szStone1
iAdrszJewels1: .int szJewels1
iAdrszStone2: .int szStone2
iAdrszJewels2: .int szJewels2
iAdrsZoneConv: .int sZoneConv
iAdrszMessResult: .int szMessResult
iAdrszCarriageReturn: .int szCarriageReturn
iAdrszMessStart: .int szMessStart
/***************************************************/
/* count jewels in stone */
/***************************************************/
/* r0 contains stone address */
/* r1 contains jewels address */
/* r0 return jewels count */
countJewels:
push {r1-r6,lr} @ save registers
mov r4,#0 @ counter
mov r3,#0 @ index stone
1:
ldrb r6,[r0,r3] @ load byte of stone
cmp r6,#0 @ end stone ?
beq 3f
mov r5,#0 @ index jewels
2:
ldrb r2,[r1,r5] @ load byte of jewels
cmp r2,#0 @ end jewels ?
addeq r3,r3,#1 @ yes -> increment index stone
beq 1b @ and loop
cmp r6,r2 @ compare byte
addne r5,r5,#1 @ not equal -> increment jewels index
bne 2b @ and loop
add r4,r4,#1 @ else increment counter
add r3,r3,#1 @ incremente index stone
b 1b @ and loop
 
3: @ result display
mov r0,r4
ldr r1,iAdrsZoneConv
bl conversion10
ldr r0,iAdrszMessResult
bl affichageMess
ldr r0,iAdrsZoneConv
bl affichageMess
ldr r0,iAdrszCarriageReturn
bl affichageMess
mov r0,r4
100:
pop {r1-r6,pc}
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
/* for this file see task include a file in language ARM assembly*/
.include "../affichage.inc"
 
</syntaxhighlight>
{{Out}}
with the file unixdict.txt
<pre>
Program 32 bits start.
Result: 3
Result: 0
</pre>
=={{header|Arturo}}==
<langsyntaxhighlight lang="rebol">count: function [jewels,stones][
size select split stones => [in? & jewels]
]
 
print count "aA" "aAAbbbb"</langsyntaxhighlight>
 
{{out}}
Line 425 ⟶ 763:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">JewelsandStones(ss, jj){
for each, jewel in StrSplit(jj)
for each, stone in StrSplit(ss)
Line 431 ⟶ 769:
num++
return num
}</langsyntaxhighlight>
Example:<langsyntaxhighlight AutoHotkeylang="autohotkey">MsgBox % JewelsandStones("aAAbbbbz", "aAZ")
return</langsyntaxhighlight>
Outputs:<pre>3</pre>
 
=={{header|AWK}}==
<langsyntaxhighlight AWKlang="awk"># syntax: GAWK -f JEWELS_AND_STONES.AWK
BEGIN {
printf("%d\n",count("aAAbbbb","aA"))
Line 451 ⟶ 789:
return(total)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>3
Line 457 ⟶ 795:
 
=={{header|BASIC}}==
<syntaxhighlight lang="basic">10 READ N%
 
<lang basic>10 READ N%
20 FOR A%=1 TO N%
30 READ J$,S$
Line 477 ⟶ 814:
210 DATA "aA","aAAbbbb"
220 DATA "z","ZZZZ"
</syntaxhighlight>
</lang>
 
{{out}}
 
<pre>aAAbbbb in aA: 3
ZZZZ in z: 0</pre>
 
==={{header|BASIC256}}===
<syntaxhighlight lang="vb">
<lang BASIC256>
function contar_joyas(piedras, joyas)
cont = 0
Line 499 ⟶ 835:
print contar_joyas("ABCDEFGHIJKLMNOPQRSTUVWXYZ@abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ@abcdefghijklmnopqrstuvwxyz")
print contar_joyas("AB", "")
</syntaxhighlight>
</lang>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
<pre>
 
Igual que la entrada de FreeBASIC.
==={{header|Chipmunk Basic}}===
</pre>
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="vb">100 cls
110 print contjoyas("aAAbbbb","aA")
120 print contjoyas("ZZ","z")
130 print contjoyas("ABCDEFGHIJKLMNOPQRSTUVWXYZ@abcdefghijklmnopqrstuvwxyz","ABCDEFGHIJKLMNOPQRSTUVWXYZ@abcdefghijklmnopqrstuvwxyz")
140 print contjoyas("AB","")
150 end
160 sub contjoyas(piedras$,joyas$)
180 sgte = 0
190 for i = 1 to len(piedras$)
200 bc = instr(joyas$,mid$(piedras$,i,1))
210 if bc <> 0 then sgte = sgte+1
220 next i
230 contjoyas = sgte
240 end sub</syntaxhighlight>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="vb">
function contar_joyas(piedras as string, joyas as string) as integer
dim as integer bc, cont = 0
for i as integer = 1 to len(piedras)
bc = instr(1, joyas, mid(piedras, i, 1))
if bc <> 0 then cont += 1
next i
contar_joyas = cont
end function
 
print contar_joyas("aAAbbbb", "aA")
print contar_joyas("ZZ", "z")
print contar_joyas("ABCDEFGHIJKLMNOPQRSTUVWXYZ@abcdefghijklmnopqrstuvwxyz", _
"ABCDEFGHIJKLMNOPQRSTUVWXYZ@abcdefghijklmnopqrstuvwxyz")
print contar_joyas("AB", "")
</syntaxhighlight>
{{out}}
<pre>3
0
53
0</pre>
 
==={{header|FutureBasic}}===
<syntaxhighlight lang="futurebasic">window 1, @"Jewels and Stones"
 
local fn JewelsAndStones( jewels as CFStringRef, stones as CFStringRef ) as long
long index, count = 0
for index = 0 to len(stones) - 1
if ( fn StringContainsString( jewels, mid(stones,index,1) ) ) then count++
next
end fn = count
 
print fn JewelsAndStones( @"aA", @"aAAbbbb" )
print fn JewelsAndStones( @"z", @"ZZ" )
 
HandleEvents</syntaxhighlight>
{{out}}
<pre>3
0</pre>
 
==={{header|GW-BASIC}}===
{{works with|QBasic}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
{{works with|MSX BASIC}}
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
{{works with|Run BASIC}}
<syntaxhighlight lang="qbasic">100 CLS
110 piedras$ = "aAAbbbb"
120 joyas$ = "aA"
130 GOSUB 240: PRINT cntjoyas
140 piedras$ = "ZZ"
150 joyas$ = "z"
160 GOSUB 240: PRINT cntjoyas
170 piedras$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ@abcdefghijklmnopqrstuvwxyz"
180 joyas$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ@abcdefghijklmnopqrstuvwxyz"
190 GOSUB 240: PRINT cntjoyas
200 piedras$ = "AB"
210 joyas$ = ""
220 GOSUB 240: PRINT cntjoyas
230 END
240 sgte = 0
250 FOR i = 1 TO LEN(piedras$)
260 bc = INSTR(joyas$, MID$(piedras$, i, 1))
270 IF bc <> 0 THEN sgte = sgte + 1
280 NEXT i
290 cntjoyas = sgte
300 RETURN
310 END</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|MSX Basic}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">FUNCTION contarjoyas (piedras$, joyas$)
cont = 0
FOR i = 1 TO LEN(piedras$)
bc = INSTR(1, joyas$, MID$(piedras$, i, 1))
IF bc <> 0 THEN cont = cont + 1
NEXT i
contarjoyas = cont
END FUNCTION
 
PRINT contarjoyas("aAAbbbb", "aA")
PRINT contarjoyas("ZZ", "z")
PRINT contarjoyas("ABCDEFGHIJKLMNOPQRSTUVWXYZ@abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ@abcdefghijklmnopqrstuvwxyz")
PRINT contarjoyas("AB", "")
END</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Run BASIC}}===
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
<syntaxhighlight lang="vb">sub contar_joyas(piedras$, joyas$)
local count, i, bc
cont = 0
for i = 1 to len(piedras$)
bc = instr(joyas$, mid$(piedras$, i, 1))
if bc <> 0 cont = cont + 1
next i
return cont
end sub
 
print contar_joyas("aAAbbbb", "aA")
print contar_joyas("ZZ", "z")
print contar_joyas("ABCDEFGHIJKLMNOPQRSTUVWXYZ@abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ@abcdefghijklmnopqrstuvwxyz")
print contar_joyas("AB", "")
end</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">FUNCTION contarjoyas(piedras$, joyas$)
LET c = 0
FOR i = 1 TO LEN(piedras$)
LET bc = POS(joyas$,(piedras$)[i:i+1-1],1)
IF bc <> 0 THEN LET c = c + 1
NEXT i
LET contarjoyas = c
END FUNCTION
 
PRINT contarjoyas("aAAbbbb", "aA")
PRINT contarjoyas("ZZ", "z")
PRINT contarjoyas("ABCDEFGHIJKLMNOPQRSTUVWXYZ@abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ@abcdefghijklmnopqrstuvwxyz")
PRINT contarjoyas("AB", "")
END</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|VBA}}===
{{trans|Phix}}<syntaxhighlight lang="vb">Function count_jewels(stones As String, jewels As String) As Integer
Dim res As Integer: res = 0
For i = 1 To Len(stones)
res = res - (InStr(1, jewels, Mid(stones, i, 1), vbBinaryCompare) <> 0)
Next i
count_jewels = res
End Function
Public Sub main()
Debug.Print count_jewels("aAAbbbb", "aA")
Debug.Print count_jewels("ZZ", "z")
End Sub</syntaxhighlight>{{out}}
<pre> 3
0 </pre>
 
==={{header|Visual Basic .NET}}===
{{trans|C#}}
<syntaxhighlight lang="vbnet">Module Module1
 
Function Count(stones As String, jewels As String) As Integer
Dim bag = jewels.ToHashSet
Return stones.Count(AddressOf bag.Contains)
End Function
 
Sub Main()
Console.WriteLine(Count("aAAbbbb", "Aa"))
Console.WriteLine(Count("ZZ", "z"))
End Sub
 
End Module</syntaxhighlight>
{{out}}
<pre>3
0</pre>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="vb">print contar_joyas("aAAbbbb", "aA")
print contar_joyas("ZZ", "z")
print contar_joyas("ABCDEFGHIJKLMNOPQRSTUVWXYZ@abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ@abcdefghijklmnopqrstuvwxyz")
print contar_joyas("AB", "")
end
sub contar_joyas(piedras$, joyas$)
local count, i, bc
cont = 0
for i = 1 to len(piedras$)
bc = instr(joyas$, mid$(piedras$, i, 1))
if bc <> 0 cont = cont + 1
next i
return cont
end sub</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
let jewels(j, s) = valof
Line 525 ⟶ 1,070:
$( show("aA", "aAAbbbb")
show("z", "ZZ")
$)</langsyntaxhighlight>
{{out}}
<pre>"aA" in "aAAbbbb": 3
"z" in "ZZ": 0</pre>
 
=={{header|BQN}}==
Similar in nature to APL, mostly due to how trivial the problem is in an array language.
 
<syntaxhighlight lang="bqn">Jewels←+´∊˜</syntaxhighlight>
<syntaxhighlight lang="text"> "aA" Jewels "aAAbbbb"
3</syntaxhighlight>
=={{header|Bracmat}}==
<langsyntaxhighlight Bracmatlang="bracmat"> ( f
= stones jewels N
. !arg:(?stones.?jewels)
Line 550 ⟶ 1,101:
)
& f$(aAAbbbb.aA)
</syntaxhighlight>
</lang>
'''Output'''
<pre>3</pre>
Line 556 ⟶ 1,107:
=={{header|C}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
 
Line 569 ⟶ 1,120:
printf("%d\n", count_jewels("ZZ", "z"));
return 0;
}</langsyntaxhighlight>
 
{{output}}
Line 578 ⟶ 1,129:
 
=={{header|C sharp}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Linq;
 
Line 592 ⟶ 1,143:
return stones.Count(bag.Contains);
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 601 ⟶ 1,152:
=={{header|C++}}==
{{trans|D}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
 
Line 621 ⟶ 1,172:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>3
0</pre>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">count_jewels = proc (jewels, stones: string) returns (int)
is_jewel: array[bool] := array[bool]$fill(0, 256, false)
for c: char in string$chars(jewels) do
is_jewel[char$c2i(c)] := true
end
n_jewels: int := 0
for c: char in string$chars(stones) do
if is_jewel[char$c2i(c)] then n_jewels := n_jewels + 1 end
end
return (n_jewels)
end count_jewels
 
show = proc (jewels, stones: string)
po: stream := stream$primary_output()
stream$putl(po, "\"" || jewels || "\" in \"" || stones || "\": "
|| int$unparse(count_jewels(jewels, stones)))
end show
 
start_up = proc ()
show("aA", "aAAbbbb")
end start_up</syntaxhighlight>
{{out}}
<pre>"aA" in "aAAbbbb": 3</pre>
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
sub count_jewels(stones: [uint8], jewels: [uint8]): (count: uint16) is
Line 655 ⟶ 1,233:
 
print_and_count("aAAbbbb", "aA");
print_and_count("ZZ", "z");</langsyntaxhighlight>
 
{{out}}
Line 663 ⟶ 1,241:
 
=={{header|Crystal}}==
<langsyntaxhighlight lang="ruby">stones, jewels = "aAAbbbb", "aA"
stones.count(jewels) # => 3
 
Line 672 ⟶ 1,250:
 
# This works as intended though:
stones.count { |c| jewels.chars.includes?(c) } # => 2</langsyntaxhighlight>
 
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="d">import std.algorithm;
import std.stdio;
 
Line 692 ⟶ 1,270:
countJewels("aAAbbbb", "aA").writeln;
countJewels("ZZ", "z").writeln;
}</langsyntaxhighlight>
{{out}}
<pre>3
0</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|classes, SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
function CommonLetters(S1,S2: string): integer;
{Count the number of letters in S1 are found in S2}
var I, J: integer;
begin
Result:=0;
for I:=1 to Length(S1) do
for J:=1 to Length(S2) do
if S1[I]=S2[J] then Inc(Result);
end;
 
procedure ShowJewelsStones(Memo: TMemo; Jewels,Stones: string);
{Show one Jewels-Stones comparison}
begin
Memo.Lines.Add(Jewels+' '+Stones+' '+IntToStr(CommonLetters(Jewels,Stones)));
end;
 
 
procedure TestJewelsStones(Memo: TMemo);
begin
ShowJewelsStones(Memo,'aAAbbbb', 'aA');
ShowJewelsStones(Memo,'ZZ','z');
end;
 
</syntaxhighlight>
{{out}}
<pre>
aAAbbbb aA 3
ZZ z 0
</pre>
 
 
=={{header|Draco}}==
<syntaxhighlight lang="draco">proc nonrec count_jewels(*char jewels, stones) word:
[256] bool jewel;
word count;
byte i;
char c;
for i from 0 upto 255 do jewel[i] := false od;
while c := jewels*; c ~= '\e' do
jewel[c] := true;
jewels := jewels + 1;
od;
count := 0;
while c := stones*; c ~= '\e' do
if jewel[c] then count := count + 1 fi;
stones := stones + 1
od;
count
corp
 
proc nonrec show(*char jewels, stones) void:
writeln("'", jewels, "' in '", stones, "': ", count_jewels(jewels, stones))
corp
 
proc nonrec main() void:
show("aA", "aAAbbbb");
show("z", "ZZ")
corp</syntaxhighlight>
{{out}}
<pre>'aA' in 'aAAbbbb': 3
'z' in 'ZZ': 0</pre>
 
=={{header|Dyalect}}==
Line 701 ⟶ 1,350:
{{trans|Swift}}
 
<langsyntaxhighlight lang="dyalect">func countJewels(stones, jewels) {
stones.iterIterate().mapMap(x => jewels.containsContains(x) ? 1 : 0).reduceReduce(0, (x,y) => x + y, 0)
}
print(countJewels("aAAbbbb", "aA"))
print(countJewels("ZZ", "z"))</langsyntaxhighlight>
 
{{out}}
Line 712 ⟶ 1,361:
<pre>3
0</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
 
func count stones$ jewels$ .
len d[] 65536
for c$ in strchars jewels$
d[strcode c$] = 1
.
for c$ in strchars stones$
if d[strcode c$] = 1
cnt += 1
.
.
return cnt
.
print count "aAAbbbb" "aA"
print count "ZZ" "z"
</syntaxhighlight>
 
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
let fN jewels stones=stones|>Seq.filter(fun n->Seq.contains n jewels)|>Seq.length
printfn "%d" (fN "aA" "aAAbbbb")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 724 ⟶ 1,393:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: kernel prettyprint sequences ;
 
: count-jewels ( stones jewels -- n ) [ member? ] curry count ;
 
"aAAbbbb" "aA"
"ZZ" "z" [ count-jewels . ] 2bi@</langsyntaxhighlight>
{{out}}
<pre>
Line 737 ⟶ 1,406:
 
=={{header|Euphoria}}==
<syntaxhighlight lang="euphoria">
<lang Euphoria>
function number_of(object jewels, object stones) -- why limit ourselves to strings?
integer ct = 0
Line 749 ⟶ 1,418:
? 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>
</lang>
{{out}}
<pre>
Line 755 ⟶ 1,424:
0
4 -- 1 is found once, "Boo" is found once, and 3 is found twice = 4 things in the search list were found in the target list
</pre>
 
=={{header|FreeBASIC}}==
<lang freebasic>
function contar_joyas(piedras as string, joyas as string) as integer
dim as integer bc, cont: cont = 0
for i as integer = 1 to len(piedras)
bc = instr(1, joyas, mid(piedras, i, 1))
if bc <> 0 then cont += 1
next i
contar_joyas = cont
end function
 
print contar_joyas("aAAbbbb", "aA")
print contar_joyas("ZZ", "z")
print contar_joyas("ABCDEFGHIJKLMNOPQRSTUVWXYZ@abcdefghijklmnopqrstuvwxyz", _
"ABCDEFGHIJKLMNOPQRSTUVWXYZ@abcdefghijklmnopqrstuvwxyz")
print contar_joyas("AB", "")
</lang>
{{out}}
<pre>
3
0
53
0
</pre>
 
Line 788 ⟶ 1,432:
 
'''Outer loop stones, index into jewels:'''
<langsyntaxhighlight lang="go">package main
import (
Line 806 ⟶ 1,450:
func main() {
fmt.Println(js("aAAbbbb", "aA"))
}</langsyntaxhighlight>
{{out}}
<pre>3</pre>
 
'''Outer loop jewels, count stones:'''
<langsyntaxhighlight lang="go">func js(stones, jewels string) (n int) {
for _, b := range []byte(jewels) {
n += strings.Count(stones, string(b))
}
return
}</langsyntaxhighlight>
 
'''Construct jewel set, then loop over stones:'''
<langsyntaxhighlight lang="go">func js(stones, jewels string) (n int) {
var jSet ['z' + 1]int
for _, b := range []byte(jewels) {
Line 828 ⟶ 1,472:
}
return
}</langsyntaxhighlight>
 
'''Construct stone multiset, then loop over jewels:'''
<langsyntaxhighlight lang="go">func js(stones, jewels string) (n int) {
var sset ['z' + 1]int
for _, b := range []byte(stones) {
Line 840 ⟶ 1,484:
}
return
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">jewelCount
:: Eq a
=> [a] -> [a] -> Int
Line 855 ⟶ 1,499:
main :: IO ()
main = mapM_ print $ uncurry jewelCount <$> [("aA", "aAAbbbb"), ("z", "ZZ")]
</syntaxhighlight>
</lang>
{{Out}}
<pre>3
Line 862 ⟶ 1,506:
Or in terms of filter rather than foldr
 
<langsyntaxhighlight lang="haskell">jewelCount
:: Eq a
=> [a] -> [a] -> Int
Line 871 ⟶ 1,515:
main = do
print $ jewelCount "aA" "aAAbbbb"
print $ jewelCount "z" "ZZ"</langsyntaxhighlight>
{{Out}}
<pre>3
Line 877 ⟶ 1,521:
 
=={{header|J}}==
<syntaxhighlight lang="j">
<lang J>
NB. jewels sums a raveled equality table
NB. use: x jewels y x are the stones, y are the jewels.
Line 891 ⟶ 1,535:
0
</syntaxhighlight>
</lang>
 
A simpler implementation, which satisfies the task requirements but does not explicitly ignore non-letters, would be:
 
<syntaxhighlight lang=J>jewels=: +/@e.</syntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.util.HashSet;
import java.util.Set;
 
Line 918 ⟶ 1,566:
System.out.println(countJewels("ZZ", "z"));
}
}</langsyntaxhighlight>
{{out}}
<pre>3
Line 924 ⟶ 1,572:
 
=={{header|Javascript}}==
<langsyntaxhighlight lang="javascript">(() => {
 
// jewelCount :: String -> String -> Int
Line 939 ⟶ 1,587:
]
.map(x => jewelCount(...x))
})();</langsyntaxhighlight>
{{Out}}
<pre>[3, 0]</pre>
 
=={{header|Joy}}==
<syntaxhighlight lang="joy">DEFINE jewels == [in 1 0 choice +] cons 0 swap fold.
 
"aAAbbbb" "aA" jewels.
"ZZ" "z" jewels.</syntaxhighlight>
{{out}}
<pre>3
0</pre>
 
=={{header|jq}}==
<langsyntaxhighlight lang="jq">$ jq -n --arg stones aAAbbbb --arg jewels aA '
[$stones|split("") as $s|$jewels|split("") as $j|$s[]|
select(. as $c|$j|contains([$c]))]|length'</langsyntaxhighlight>
{{out}}
 
{{Out}}
<pre>3</pre>
 
Line 954 ⟶ 1,610:
 
'''Module''':
<langsyntaxhighlight lang="julia">module Jewels
 
count(s, j) = Base.count(x ∈ j for x in s)
 
end # module Jewels</langsyntaxhighlight>
 
'''Main''':
<langsyntaxhighlight lang="julia">@show Jewels.count("aAAbbbb", "aA")
@show Jewels.count("ZZ", "z")</langsyntaxhighlight>
 
{{out}}
<pre>Jewels.count("aAAbbbb", "aA") = 3
Jewels.count("ZZ", "z") = 0</pre>
 
=={{header|K}}==
<syntaxhighlight lang="k">jewels: +/~^?
 
jewels["aA";"aAAbbbb"]</syntaxhighlight>
{{out}}
<pre>3</pre>
 
=={{header|Kotlin}}==
<langsyntaxhighlight scalalang="kotlin">// Version 1.2.40
 
fun countJewels(s: String, j: String) = s.count { it in j }
Line 976 ⟶ 1,639:
println(countJewels("aAAbbbb", "aA"))
println(countJewels("ZZ", "z"))
}</langsyntaxhighlight>
{{out}}
 
{{output}}
<pre>
3
Line 985 ⟶ 1,647:
 
=={{header|Lambdatalk}}==
<langsyntaxhighlight lang="scheme">
{def countjewels
{def countjewels.r
Line 1,002 ⟶ 1,664:
{countjewels aAAbbbb aA} -> 3
{countjewels ZZ z} -> 0
</syntaxhighlight>
</lang>
 
=={{header|Lua}}==
{{trans|C}}
<langsyntaxhighlight lang="lua">function count_jewels(s, j)
local count = 0
for i=1,#s do
Line 1,018 ⟶ 1,680:
 
print(count_jewels("aAAbbbb", "aA"))
print(count_jewels("ZZ", "z"))</langsyntaxhighlight>
{{out}}
<pre>3
0</pre>
 
=={{header|MACRO-11}}==
<syntaxhighlight lang="macro11"> .TITLE JWLSTN
.MCALL .TTYOUT,.EXIT
JWLSTN::JMP DEMO
 
; COUNT JEWELS R1 IN STONES R0, ANSWER IN R2
COUNT: MOV #200,R2 ; CLEAR JEWELS
MOV #6$,R3
1$: CLR (R3)+
SOB R2,1$
BR 3$
2$: MOVB #1,6$(R2) ; MARK JEWELS
3$: MOVB (R1)+,R2
BNE 2$
BR 5$
4$: MOVB 6$(R1),R3 ; COUNT JEWELS IN STONES
ADD R3,R2
5$: MOVB (R0)+,R1
BNE 4$
RTS PC
6$: .BLKB 400
 
DEMO: MOV #TESTS,R4
1$: MOV (R4)+,R1 ; READ TEST CASE
MOV (R4)+,R0
BEQ 2$ ; DONE?
JSR PC,COUNT ; COUNT
MOV R2,R0 ; DISPLAY RESULT
ADD #60,R0
.TTYOUT
MOV #15,R0
.TTYOUT
MOV #12,R0
.TTYOUT
BR 1$ ; NEXT
2$: .EXIT
 
TESTS: .WORD 1$,2$,3$,4$,0,0
1$: .ASCIZ /aA/
2$: .ASCIZ /aAAbbbb/
3$: .ASCIZ /z/
4$: .ASCIZ /ZZ/
.END JWLSTN</syntaxhighlight>
{{out}}
<pre>3
Line 1,024 ⟶ 1,733:
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">count_jewel := proc(stones, jewels)
local count, j, letter:
j := convert(jewels,set):
Line 1,035 ⟶ 1,744:
return count:
end proc:
count_jewel("aAAbbbb", "aA")</langsyntaxhighlight>
{{Out|Output}}
<pre>3</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">JewelsStones[j_String, s_String] := Count[MemberQ[Characters[j], #] & /@ Characters[s], True]
JewelsStones["aA", "aAAbbbb"]
JewelsStones["ZZ", "z"]</langsyntaxhighlight>
{{out}}
<pre>3
Line 1,048 ⟶ 1,757:
 
=={{header|MATLAB}} / {{header|Octave}}==
<syntaxhighlight lang="matlab">
<lang Matlab>
function s = count_jewels(stones,jewels)
s=0;
Line 1,058 ⟶ 1,767:
%!test
%! assert(count_jewels('ZZ','z'),0)
</syntaxhighlight>
</lang>
 
=={{header|min}}==
{{works with|min|0.19.6}}
<langsyntaxhighlight lang="min">(('' '' '') => spread if) :if?
 
((1 0 if?) concat map sum) :count
Line 1,071 ⟶ 1,780:
 
"aAAbbbb" "aA" count-jewels puts!
"ZZ" "z" count-jewels puts!</langsyntaxhighlight>
{{out}}
<pre>
Line 1,079 ⟶ 1,788:
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE Jewels;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 1,114 ⟶ 1,823:
 
ReadChar
END Jewels.</langsyntaxhighlight>
{{out}}
<pre>3
Line 1,120 ⟶ 1,829:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import sequtils
 
func countJewels(stones, jewels: string): Natural =
Line 1,126 ⟶ 1,835:
 
echo countJewels("aAAbbbb", "aA")
echo countJewels("ZZ", "z")</langsyntaxhighlight>
 
{{out}}
Line 1,134 ⟶ 1,843:
=={{header|Objeck}}==
{{trans|Java}}
<langsyntaxhighlight Objecklang="objeck">use Collection.Generic;
 
class JewelsStones {
Line 1,158 ⟶ 1,867:
return count;
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,165 ⟶ 1,874:
0
</pre>
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let jewels j s =
String.(fold_left (fun n c -> if contains j c then succ n else n) 0 s)</syntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">sub count_jewels {
my( $j, $s ) = @_;
my($c,%S);
Line 1,177 ⟶ 1,890:
 
print count_jewels 'aA' , 'aAAbbbb';
print count_jewels 'z' , 'ZZ';</langsyntaxhighlight>
{{out}}
<pre>3
0</pre>
===Alternate using regex===
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Jewels_and_Stones#Perl
Line 1,193 ⟶ 1,906:
aAAbbbb abc
ZZ z
END</langsyntaxhighlight>
{{out}}
<pre>
Line 1,202 ⟶ 1,915:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="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: #004080;">integer</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
Line 1,212 ⟶ 1,925:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,218 ⟶ 1,931:
0
</pre>
 
=={{header|Picat}}==
===List comprehension===
 
<syntaxhighlight lang="picat">jewels_and_stones1(Jewels,Stones) = sum([1 : S in Stones, J in Jewels, S == J]).</syntaxhighlight>
 
===Recursion===
<syntaxhighlight lang="picat">jewels_and_stones2(Jewels,Stones) = N =>
jewels_and_stones2(Jewels,Stones,0,N).
 
jewels_and_stones2([],_Stones,N,N).
jewels_and_stones2([J|Jewels],[S|Stones],N0,N) :-
jewels_and_stones2_(J,[S|Stones],0,JN),
jewels_and_stones2(Jewels,Stones,N0+JN,N).
 
% Check just this jewel on all the stones
jewels_and_stones2_(_J,[],N,N).
jewels_and_stones2_(J,[S|Stones],N0,N) :-
( J == S ->
N1 = N0+1
;
N1 = N0
),
jewels_and_stones2_(J,Stones,N1,N).</syntaxhighlight>
 
===Foreach loop===
<syntaxhighlight lang="picat">jewels_and_stones3(Jewels,Stones) = N =>
N = 0,
foreach(J in Jewels, S in Stones)
if J == S then
N := N + 1
end
end.</syntaxhighlight>
 
===Test===
<syntaxhighlight lang="picat">go =>
Tests = [["aA","aAAbbbb"],
["z","ZZ"]
],
println(tests=Tests),
foreach([Jewels,Stones] in Tests)
println([jewels=Jewels,stone=Stones]),
println(js1=jewels_and_stones1(Jewels,Stones)),
println(js2=jewels_and_stones2(Jewels,Stones)),
println(js3=jewels_and_stones3(Jewels,Stones)),
nl
end,
nl.</syntaxhighlight>
 
{{out}}
<pre>tests = [[aA,aAAbbbb],[z,ZZ]]
[jewels = aA,stone = aAAbbbb]
js1 = 3
js2 = 3
js3 = 3
 
[jewels = z,stone = ZZ]
js1 = 0
js2 = 0
js3 = 0</pre>
 
===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).
<syntaxhighlight lang="picat">go2 =>
Alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
Len = Alpha.len,
_ = random2(),
NumStones = 100_000_000,
NumJewels = 15, % Atmost number of jewels (duplicates are removed)
Stones = [Alpha[random(1,Len)] : _ in 1..NumStones],
Jewels = [Alpha[random(1,Len)] : _ in 1..NumJewels].sort_remove_dups,
println(jewels=Jewels),
nl,
time(println(js1=jewels_and_stones1(Jewels,Stones))),
time(println(js2=jewels_and_stones2(Jewels,Stones))),
time(println(js3=jewels_and_stones3(Jewels,Stones))),
nl.</syntaxhighlight>
 
{{out}}
<pre>NumStones: 100_000_000 NumJewels = 15
jewels = TwurtRabSXx
 
js1 = 21154798
 
CPU time 15.087 seconds.
 
js2 = 21154796
 
CPU time 11.024 seconds.
 
js3 = 21154798
 
CPU time 11.94 seconds.</pre>
 
The recursion approach (<code>jewels_and_stones2/2</code>) is a little faster than the loop approach (<code>jewels_and_stones/3</code>).
 
 
=={{header|PL/M}}==
<langsyntaxhighlight lang="plm">100H:
 
/* FIND JEWELS AMONG STONES */
Line 1,295 ⟶ 2,105:
 
CALL BDOS(0,0);
EOF</langsyntaxhighlight>
{{out}}
<pre>'aA' IN 'aAAbbbb': 3
Line 1,302 ⟶ 2,112:
=={{header|Prolog}}==
 
<langsyntaxhighlight lang="prolog">
 
:- system:set_prolog_flag(double_quotes,codes) .
Line 1,312 ⟶ 2,122:
.
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,333 ⟶ 2,143:
 
{{works with|SWI Prolog}}
<langsyntaxhighlight lang="prolog">count_jewels(Stones, Jewels, N):-
string_codes(Stones, Scodes),
string_codes(Jewels, Jcodes),
Line 1,351 ⟶ 2,161:
count_jewels([S|Stones], Jewels, N, R).
count_jewels([_|Stones], Jewels, N, R):-
count_jewels(Stones, Jewels, N, R).</langsyntaxhighlight>
 
{{out}}
Line 1,367 ⟶ 2,177:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">def countJewels(s, j):
return sum(x in j for x in s)
 
print countJewels("aAAbbbb", "aA")
print countJewels("ZZ", "z")</langsyntaxhighlight>
{{Out}}
<pre>3
Line 1,377 ⟶ 2,187:
 
===Python 3 Alternative===
<langsyntaxhighlight lang="python">def countJewels(stones, jewels):
jewelset = set(jewels)
return sum(1 for stone in stones if stone in jewelset)
 
print(countJewels("aAAbbbb", "aA"))
print(countJewels("ZZ", "z"))</langsyntaxhighlight>
{{Out}}
<pre>3
0</pre>
 
=={{header|R}}==
<syntaxhighlight lang="r">J_n_S <- function(stones ="aAAbbbb", jewels = "aA") {
stones <- unlist(strsplit(stones, split = "")) # obtain a character vector
jewels <- unlist(strsplit(jewels, split = ""))
count <- sum(stones %in% jewels)
}
 
print(J_n_S("aAAbbbb", "aA"))
print(J_n_S("ZZ", "z"))
print(J_n_S("lgGKJGljglghGLGHlhglghoIPOgfdtrdDCHnvbnmBVC", "fFgGhH"))
</syntaxhighlight>
{{Out}}
<pre>> print(J_n_S("aAAbbbb", "aA"))
[1] 3
> print(J_n_S("ZZ", "z"))
[1] 0
> print(J_n_S("lgGKJGljglghGLGHlhglghoIPOgfdtrdDCHnvbnmBVC", "fFgGhH"))
[1] 16
</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery"> [ 0 0 rot
witheach [ bit | ]
rot witheach
[ bit over & if
[ dip 1+ ] ]
drop ] is j&s ( $ $ --> n )
 
$ "aAAbbbb" $ "aA" j&s echo</syntaxhighlight>
 
{{out}}
 
<pre>3</pre>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
 
(define (jewels-and-stones stones jewels)
Line 1,396 ⟶ 2,241:
(jewels-and-stones "aAAbbbb" "aA")
(jewels-and-stones "ZZ" "z"))
</syntaxhighlight>
</lang>
{{out}}
<pre>3
Line 1,403 ⟶ 2,248:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>sub count-jewels ( Str $j, Str $s --> Int ) {
my %counts_of_all = $s.comb.Bag;
my @jewel_list = $j.comb.unique;
Line 1,411 ⟶ 2,256:
 
say count-jewels 'aA' , 'aAAbbbb';
say count-jewels 'z' , 'ZZ';</langsyntaxhighlight>
{{Out}}
<pre>3
0</pre>
 
=={{header|Red}}==
<syntaxhighlight lang="rebol">Red [
title: "Jewels and stones"
red-version: 0.6.4
]
 
count: function [
"Returns the number of values in a block for which a function returns true"
values [any-list! string!] "The values from which to count"
fn [function!] "A function that returns true or false"
][
count: 0
foreach value values [if fn value [count: count + 1]]
count
]
 
count-jewels: function [
"Returns the count of 'jewels' in the 'stones'"
stones "The values to search for jewels"
jewels "The values to count in the stones"
][
result: 0
foreach jewel jewels [
result: result + count stones function [stone][stone = jewel]
]
]
 
print count-jewels "aAAbbbb" "aA"
print count-jewels "ZZ" "z"</syntaxhighlight>
{{out}}
<pre>
3
0
</pre>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Prout <Jewels ('aAAbbb') ('aA')>>
<Prout <Jewels ('ZZ') ('z')>>;
};
 
Jewels {
() (e.Jewels)
= 0;
(s.Stone e.Stones) (e.Jewels), e.Jewels: e.X s.Stone e.Y
= <+ 1 <Jewels (e.Stones) (e.Jewels)>>;
(s.Stone e.Stones) (e.Jewels)
= <Jewels (e.Stones) (e.Jewels)>;
};</syntaxhighlight>
{{out}}
<pre>3
0</pre>
Line 1,418 ⟶ 2,316:
=={{header|REXX}}==
Programming note: &nbsp; a check is made so that only (Latin) letters are counted as a match.
<langsyntaxhighlight 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('ZZ' , "z" )
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
count: procedure
count: procedure; parse arg stones,jewels /*obtain the two strings specified. */
parse arg stones,jewels /*obtain the two strings specified. */
#= 0 /*initialize the variable # to zero.*/
number= 0 do j=1 for length(stones) /*scaninitialize STONESnumber for matchingto JEWELS charszero.*/
do j=1 for length(stones) /*scan STONES for matching JEWELS chars*/
x= substr(stones, j, 1) /*obtain a character of the STONES var.*/
x= substr(stones, j, 1) /*obtain a character of the STONES var.*/
if datatype(x, 'M') then if pos(x, jewels)\==0 then #= # + 1
if datatype(x, 'M') then if pos(x, jewels)\==0 then number=number + 1
end /*j*/ /* [↑] if a letter and a match, bump #*/
end /*j*/ return # /* [↑] if a letter and a match, /*return thebump number of common letters. */</lang>
return number /*return the number of common letters. */
</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>3
Line 1,435 ⟶ 2,335:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring"># Project Jewels and Stones
jewels = "aA"
Line 1,453 ⟶ 2,353:
next
return num
</syntaxhighlight>
</lang>
Output:
<pre>3
0</pre>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
≪ → stones jewels
≪ 0 1 jewels SIZE FOR j
stones jewels j j SUB
WHILE DUP2 POS REPEAT
LAST 1 + ROT SWAP OVER SIZE SUB SWAP ROT 1 + ROT ROT
END
DROP2
NEXT
'JnS' STO
"aAAbbbb" "aA" JnS
"ZZ" "z" JnS
{{out}}
<pre>
2:3
1:0
</pre>
 
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">stones, jewels = "aAAbbbb", "aA"
 
stones.count(jewels) # => 3
</syntaxhighlight>
</lang>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn count_jewels(stones: &str, jewels: &str) -> u8 {
let mut count: u8 = 0;
for cur_char in stones.chars() {
Line 1,478 ⟶ 2,400:
println!("{}", count_jewels("ZZ", "z"));
}
</syntaxhighlight>
</lang>
Output:<pre>3
0</pre>
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">object JewelsStones extends App {
def countJewels(s: String, j: String): Int = s.count(i => j.contains(i))
 
println(countJewels("aAAbbbb", "aA"))
println(countJewels("ZZ", "z"))
}</langsyntaxhighlight>
{{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|SETL}}==
<syntaxhighlight lang="setl">program count_jewels;
show("aA", "aAAbbbb");
show("z", "ZZ");
 
proc show(jewels, stones);
print("'" + jewels + "' in '" + stones + "': " + count(jewels, stones));
end proc;
 
proc count(jewels, stones);
jewels := {j : j in jewels};
return #[s : s in stones | s in jewels];
end proc;
end program;</syntaxhighlight>
{{out}}
<pre>'aA' in 'aAAbbbb': 3
'z' in 'ZZ': 0</pre>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func countJewels(s, j) {
s.chars.count { |c|
j.contains(c)
Line 1,499 ⟶ 2,439:
 
say countJewels("aAAbbbb", "aA") #=> 3
say countJewels("ZZ", "z") #=> 0</langsyntaxhighlight>
 
=={{header|Snobol}}==
<langsyntaxhighlight lang="snobol">* See how many jewels are among the stones
DEFINE('JEWELS(JWL,STN)') :(JEWELS_END)
JEWELS JEWELS = 0
Line 1,514 ⟶ 2,454:
* Example with no jewels (prints 0)
OUTPUT = JEWELS('z','ZZ')
END</langsyntaxhighlight>
 
{{out}}
Line 1,522 ⟶ 2,462:
 
=={{header|SQL}}==
<langsyntaxhighlight SQLlang="sql">-- See how many jewels are among the stones
declare @S varchar(1024) = 'AaBbCcAa'
, @J varchar(1024) = 'aA';
Line 1,555 ⟶ 2,495:
end
print 'J='+@J+' S='+@S+' TOTAL = '+cast(@TCNT as varchar(8));
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,563 ⟶ 2,503:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">func countJewels(_ stones: String, _ jewels: String) -> Int {
return stones.map({ jewels.contains($0) ? 1 : 0 }).reduce(0, +)
}
 
print(countJewels("aAAbbbb", "aA"))
print(countJewels("ZZ", "z"))</langsyntaxhighlight>
 
{{out}}
Line 1,576 ⟶ 2,516:
 
=={{header|Tcl}}==
<langsyntaxhighlight Tcllang="tcl">proc shavej {stones jewels} {
set n 0
foreach c [split $stones {}] {
Line 1,584 ⟶ 2,524:
}
puts [shavej aAAbbbb aA]
puts [shavej ZZ z]</langsyntaxhighlight>
{{out}}
3
Line 1,590 ⟶ 2,530:
 
=={{header|Terraform}}==
<langsyntaxhighlight lang="hcl">variable "jewels" {
default = "aA"
}
Line 1,606 ⟶ 2,546:
output "jewel_count" {
value = length(local.found_jewels)
}</langsyntaxhighlight>
 
{{Out}}
Line 1,626 ⟶ 2,566:
</pre>
 
=={{header|VBATransd}}==
<syntaxhighlight lang="scheme">#lang transd
{{trans|Phix}}<lang vb>Function count_jewels(stones As String, jewels As String) As Integer
Dim res As Integer: res = 0
For i = 1 To Len(stones)
res = res - (InStr(1, jewels, Mid(stones, i, 1), vbBinaryCompare) <> 0)
Next i
count_jewels = res
End Function
Public Sub main()
Debug.Print count_jewels("aAAbbbb", "aA")
Debug.Print count_jewels("ZZ", "z")
End Sub</lang>{{out}}
<pre> 3
0 </pre>
 
MainModule: {
=={{header|Visual Basic .NET}}==
countJewels: (λ j String() st String() locals: n 0
{{trans|C#}}
(for s in st do
<lang vbnet>Module Module1
(if (contains j s) (+= n 1)))
(ret n)
),
_start: (λ (lout (countJewels "aA" "aAAbbbb"))
(lout (countJewels "b" "BB")))
}</syntaxhighlight>
{{out}}
<pre>
3
0
</pre>
 
=={{header|V (Vlang)}}==
Function Count(stones As String, jewels As String) As Integer
{{trans|Go}}
Dim bag = jewels.ToHashSet
<syntaxhighlight lang="v (vlang)">fn js(stones string, jewels string) int {
Return stones.Count(AddressOf bag.Contains)
mut n := 0
End Function
for b in stones.bytes() {
if jewels.index_u8(b) >= 0 {
n++
}
}
return n
}
fn main() {
println(js("aAAbbbb", "aA"))
}</syntaxhighlight>
 
Sub Main()
Console.WriteLine(Count("aAAbbbb", "Aa"))
Console.WriteLine(Count("ZZ", "z"))
End Sub
 
End Module</lang>
{{out}}
<pre>3
3
0</pre>
</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
<langsyntaxhighlight ecmascriptlang="wren">var countJewels = Fn.new { |s, j| s.count { |c| j.contains(c) } }
 
System.print(countJewels.call("aAAbbbb", "aA"))
System.print(countJewels.call("ZZ", "z"))</langsyntaxhighlight>
 
{{out}}
Line 1,674 ⟶ 2,619:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">string 0; \Use zero-terminated strings
 
func Count(Stones, Jewels);
Line 1,696 ⟶ 2,641:
IntOut(0, Count("ZZ", "z")); CrLf(0);
IntOut(0, Count("pack my box with five dozen liquor jugs", "aeiou")); CrLf(0);
]</langsyntaxhighlight>
 
{{out}}
Line 1,706 ⟶ 2,651:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn countJewels(a,b){ a.inCommon(b).len() }</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">println(countJewels("aAAbbbb", "aA"));
println(countJewels("ZZ", "z"));</langsyntaxhighlight>
{{out}}
<pre>3
2,094

edits