How to resolve the algorithm General FizzBuzz step by step in the Caché ObjectScript programming language
How to resolve the algorithm General FizzBuzz step by step in the Caché ObjectScript programming language
Table of Contents
Problem Statement
Write a generalized version of FizzBuzz that works for any list of factors, along with their words. This is basically a "fizzbuzz" implementation where the user supplies the parameters. The user will enter the max number, then they will enter the factors to be calculated along with the corresponding word to be printed. For simplicity's sake, assume the user will input an integer as the max number and 3 factors, each with a word associated with them.
For example, given: In other words: For this example, print the numbers 1 through 20, replacing every multiple of 3 with "Fizz", every multiple of 5 with "Buzz", and every multiple of 7 with "Baxx". In the case where a number is a multiple of at least two factors, print each of the words associated with those factors in the order of least to greatest factor. For instance, the number 15 is a multiple of both 3 and 5; print "FizzBuzz". If the max number was 105 instead of 20, you would print "FizzBuzzBaxx" because it's a multiple of 3, 5, and 7.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm General FizzBuzz step by step in the Caché ObjectScript programming language
Source code in the caché programming language
GENFIZBUZZ(MAX,WORDS,NUMBERS)
; loop until max, casting numeric to avoid errors
for i=1:1:+MAX {
set j = 1
set descr = ""
; assumes NUMBERS parameter is comma-delimited
while (j <= $length(NUMBERS,",")) {
if ((i # $piece(NUMBERS,",",j,j)) = 0) {
; build descriptor string, again assuming comma-delimited WORDS parameter
set descr = descr_$piece(WORDS,",",j,j)
}
set j = j + 1
} ; check list of numbers
; output values to screen
write " "_$case(descr,"":i,:descr)
} ; next value until MAX
quit
You may also check:How to resolve the algorithm Harshad or Niven series step by step in the Craft Basic programming language
You may also check:How to resolve the algorithm Roman numerals/Encode step by step in the Racket programming language
You may also check:How to resolve the algorithm Empty directory step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm User input/Graphical step by step in the BaCon programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the Forth programming language