Jewels and stones: Difference between revisions
m (→{{header|REXX}}: added the REXX computer programming language.) |
|||
Line 133: | Line 133: | ||
<pre>3 |
<pre>3 |
||
0</pre> |
0</pre> |
||
=={{header|REXX}}== |
|||
This REXX program accepts two characters strings from the command line instead of hardcoding the strings. |
|||
<lang rexx>/*REXX pgm counts how many letters (in the 1st string) are in common with the 2nd string*/ |
|||
parse arg stones jewels . /*obtain optional arguments from the CL*/ |
|||
if stones=='' then stones= "aAAbbbb" /*Not defined? Then assume the default*/ |
|||
if jewels=='' then jewels= "aA" /* " " " " " " */ |
|||
say 'stones:' stones /*display value of the variable: STONES*/ |
|||
say 'jewels:' jewels /* " " " " " JEWELS*/ |
|||
#=0 /*initalize the variable # to zero. */ |
|||
do j=1 for length(stones) /*scan STONES for matching JEWELS chars*/ |
|||
x=substr(stones, j, 1) /*obtain a character of the STONES var.*/ |
|||
if datatype(x, 'M') & pos(x, jewels)\==0 then #=# + 1 |
|||
end /*j*/ /*if a letter & a match, bump # counter*/ |
|||
say /*display a blank line, and show number*/ |
|||
say # /*stick a fork in it, we're all done. */</lang> |
|||
{{out|output|text= when using the default input:}} |
|||
<pre> |
|||
stones: aAAbbbb |
|||
jewels: aA |
|||
3 |
|||
</pre> |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |
Revision as of 20:16, 14 May 2018
- Task
Create a function which takes two string parameters: 'stones' and 'jewels' and returns an integer.
Both strings can contain any number of upper or lower case letters. However, in the case of 'jewels', all letters must be distinct.
The function should count (and return) how many 'stones' are 'jewels' or, in other words, how many letters in 'stones' are also letters in 'jewels'.
Note that:
1. A lower case letter is considered to be different to its upper case equivalent for this purpose i.e. 'a' != 'A'.
2. The parameters do not need to have exactly the same names.
3. Validating the arguments is unnecessary.
So, for example, if passed "aAAbbbb" for 'stones' and "aA" for 'jewels', the function should return 3.
This task was inspired by this problem.
AWK
<lang AWK>
- syntax: GAWK -f JEWELS_AND_STONES.AWK
BEGIN {
printf("%d\n",count("aAAbbbb","aA")) printf("%d\n",count("ZZ","z")) exit(0)
} function count(stone,jewel, i,total) {
for (i=1; i<length(stone); i++) { if (jewel ~ substr(stone,i,1)) { total++ } } return(total)
} </lang>
- Output:
3 0
C
<lang c>#include <stdio.h>
- include <string.h>
int count_jewels(const char *s, const char *j) {
int count = 0; for ( ; *s; ++s) if (strchr(j, *s)) ++count; return count;
}
int main() {
printf("%d\n", count_jewels("aAAbbbb", "aA")); printf("%d\n", count_jewels("ZZ", "z")); return 0;
}</lang>
- Output:
3 0
Factor
<lang factor>USING: kernel prettyprint sequences ;
- count-jewels ( stones jewels -- n ) [ member? ] curry count ;
"aAAbbbb" "aA" "ZZ" "z" [ count-jewels . ] 2bi@</lang>
- Output:
3 0
Kotlin
<lang scala>// Version 1.2.40
fun countJewels(s: String, j: String) = s.count { it in j }
fun main(args: Array<String>) {
println(countJewels("aAAbbbb", "aA")) println(countJewels("ZZ", "z"))
}</lang>
- Output:
3 0
Perl 6
<lang perl6>sub count-jewels ( Str $j, Str $s --> Int ) {
my %counts_of_all = $s.comb.Bag; my @jewel_list = $j.comb.unique;
return %counts_of_all ∩ @jewel_list.Bag ?? %counts_of_all{ @jewel_list }.sum !! 0;
}
say count-jewels 'aA' , 'aAAbbbb'; say count-jewels 'z' , 'ZZ';</lang>
- Output:
3 0
Python
<lang python>def countJewels(s, j):
return sum(x in j for x in s)
print countJewels("aAAbbbb", "aA") print countJewels("ZZ", "z")</lang>
- Output:
3 0
Racket
<lang racket>
- lang racket
(define (jewels-and-stones stones jewels)
(length (filter (curryr member (string->list jewels)) (string->list stones))))
(module+ main
(jewels-and-stones "aAAbbbb" "aA") (jewels-and-stones "ZZ" "z"))
</lang>
- Output:
3 0
REXX
This REXX program accepts two characters strings from the command line instead of hardcoding the strings. <lang rexx>/*REXX pgm counts how many letters (in the 1st string) are in common with the 2nd string*/ parse arg stones jewels . /*obtain optional arguments from the CL*/ if stones== then stones= "aAAbbbb" /*Not defined? Then assume the default*/ if jewels== then jewels= "aA" /* " " " " " " */ say 'stones:' stones /*display value of the variable: STONES*/ say 'jewels:' jewels /* " " " " " JEWELS*/
- =0 /*initalize the variable # to zero. */
do j=1 for length(stones) /*scan STONES for matching JEWELS chars*/ x=substr(stones, j, 1) /*obtain a character of the STONES var.*/ if datatype(x, 'M') & pos(x, jewels)\==0 then #=# + 1 end /*j*/ /*if a letter & a match, bump # counter*/
say /*display a blank line, and show number*/ say # /*stick a fork in it, we're all done. */</lang>
- output when using the default input:
stones: aAAbbbb jewels: aA 3
Ring
<lang ring>
- Project Jewels and Stones
- Date 2018/04/25
- Author Gal Zsolt (~ CalmoSoft ~)
- Email <calmosoft@gmail.com>
jewels = "aA" stones = "aAAbbbb" see jewelsandstones(jewels,stones) + nl jewels = "z" stones = "ZZ" see jewelsandstones(jewels,stones) + nl
func jewelsandstones(jewels,stones)
num = 0 for n = 1 to len(stones) pos = substr(jewels,stones[n]) if pos > 0 num = num + 1 ok next return num
</lang> Output:
3 0
Sidef
<lang ruby>func countJewels(s, j) {
s.chars.count { |c| j.contains(c) }
}
say countJewels("aAAbbbb", "aA") #=> 3 say countJewels("ZZ", "z") #=> 0</lang>
zkl
<lang zkl>fcn countJewels(a,b){ a.inCommon(b).len() }</lang> <lang zkl>println(countJewels("aAAbbbb", "aA")); println(countJewels("ZZ", "z"));</lang>
- Output:
3 0