How to resolve the algorithm General FizzBuzz step by step in the Mathematica/Wolfram Language programming language
How to resolve the algorithm General FizzBuzz step by step in the Mathematica/Wolfram Language 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 Mathematica/Wolfram Language programming language
Source Code Breakdown:
1. Creating a List of Rules:
list={{5,"Buzz"},{3,"Fizz"},{7,"Baxx"}}
This list defines a set of rules that specify which strings to print for specific numbers. For example, the rule {5,"Buzz"}
says that when the number is divisible by 5, the string "Buzz" should be printed.
2. Calculating the Run-To Value:
runTo=(*LCM@@list[[All,1]]+1*)20
The runTo
variable determines the highest number up to which the loop will run. It is calculated as the least common multiple (LCM) of all the numbers in the list, plus one (to include the largest number itself). The (* ... *)
notation is used to suppress the output of the LCM calculation.
3. Table Creation:
Column@Table[
Select[list,Mod[x,#[[1]]]==0&][[All,2]]/.{}->{x}
,{x,1,runTo}
]
This code uses the Table
function to create a table of output values. The x
variable ranges from 1 to runTo
. For each value of x
, the Select
function is used to find all rules in the list
where the number x
is divisible by the rule's first element (e.g., Mod[x,5]==0
). If any matches are found, their second elements (e.g., "Buzz") are selected using [[All,2]]
. If no matches are found, an empty list {}
is selected.
The /.{}->{x}
expression replaces any empty lists with the current value of x
.
4. Column Output:
The resulting table is then displayed as a column using the Column
function. This arranges the output values vertically, making it easy to read the results.
Output Explanation:
For each number in the range 1 to 20, the program checks if it is divisible by any of the numbers in the list. If it is, the corresponding string from the list is printed. If not, the number itself is printed.
Here is the output for the given list
and runTo
value of 20:
1
2
Fizz
4
Buzz
Fizz
Baxx
8
Fizz
Buzz
11
Fizz
Baxx
14
FizzBuzz
16
17
Fizz
19
Buzz
Source code in the wolfram programming language
list={{5,"Buzz"},{3,"Fizz"},{7,"Baxx"}};
runTo=(*LCM@@list[[All,1]]+1*)20;
Column@Table[
Select[list,Mod[x,#[[1]]]==0&][[All,2]]/.{}->{x}
,{x,1,runTo}
]
You may also check:How to resolve the algorithm Balanced brackets step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Binary search step by step in the Ring programming language
You may also check:How to resolve the algorithm Convert seconds to compound duration step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Find the intersection of a line with a plane step by step in the Nim programming language
You may also check:How to resolve the algorithm Polynomial long division step by step in the 11l programming language