How to resolve the algorithm Jewels and stones step by step in the REXX programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Jewels and stones step by step in the REXX programming language

Table of Contents

Problem Statement

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: So, for example, if passed "aAAbbbb" for 'stones' and "aA" for 'jewels', the function should return 3. This task was inspired by this problem.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Jewels and stones step by step in the REXX programming language

Source code in the rexx programming language

/*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;  parse arg stones,jewels       /*obtain the two strings specified.    */
       #= 0                                      /*initialize 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')  then if pos(x, jewels)\==0  then #= # + 1
                   end   /*j*/                   /* [↑]  if a letter and a match, bump #*/
       return #                                  /*return the number of common letters. */


  

You may also check:How to resolve the algorithm Pig the dice game step by step in the C# programming language
You may also check:How to resolve the algorithm Dynamic variable names step by step in the Lasso programming language
You may also check:How to resolve the algorithm Idiomatically determine all the lowercase and uppercase letters step by step in the Quackery programming language
You may also check:How to resolve the algorithm SHA-256 step by step in the Phix programming language
You may also check:How to resolve the algorithm Vector products step by step in the Crystal programming language