Jewels and stones: Difference between revisions

add ABC
No edit summary
(add ABC)
 
(10 intermediate revisions by 8 users not shown)
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}}==
<syntaxhighlight lang="8080asm"> org 100h
jmp demo
Line 110 ⟶ 210:
 
<pre>3</pre>
 
 
=={{header|8086 Assembly}}==
Line 172 ⟶ 271:
 
<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}}==
Line 412 ⟶ 640:
{{output}}
<syntaxhighlight lang="applescript">6</syntaxhighlight>
=={{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}}==
<syntaxhighlight lang="rebol">count: function [jewels,stones][
Line 499 ⟶ 837:
</syntaxhighlight>
{{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}}===
Line 544 ⟶ 897:
<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}}===
Line 971 ⟶ 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#}}==
Line 1,213 ⟶ 1,623:
<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}}==
<syntaxhighlight lang="scalakotlin">// Version 1.2.40
 
fun countJewels(s: String, j: String) = s.count { it in j }
Line 1,223 ⟶ 1,640:
println(countJewels("ZZ", "z"))
}</syntaxhighlight>
{{out}}
 
{{output}}
<pre>
3
Line 1,879 ⟶ 2,295:
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>
 
=={{header|REXX}}==
Line 1,885 ⟶ 2,319:
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. */</syntaxhighlight>
return number /*return the number of common letters. */
</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>3
Line 1,976 ⟶ 2,412:
}</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)].
 
=={{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}}==
Line 2,153 ⟶ 2,607:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascriptwren">var countJewels = Fn.new { |s, j| s.count { |c| j.contains(c) } }
 
System.print(countJewels.call("aAAbbbb", "aA"))
2,093

edits