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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Jewels and stones step by step in the Racket 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 Racket programming language

Source code in the racket programming language

#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"))


  

You may also check:How to resolve the algorithm UTF-8 encode and decode step by step in the Ada programming language
You may also check:How to resolve the algorithm Bin given limits step by step in the Nim programming language
You may also check:How to resolve the algorithm Extensible prime generator step by step in the VBA programming language
You may also check:How to resolve the algorithm Chaos game step by step in the zkl programming language
You may also check:How to resolve the algorithm Combinations with repetitions step by step in the Phix programming language