How to resolve the algorithm General FizzBuzz step by step in the ALGOL 68 programming language
How to resolve the algorithm General FizzBuzz step by step in the ALGOL 68 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 ALGOL 68 programming language
Source code in the algol programming language
BEGIN # generalised FizzBuzz #
# prompts for an integer, reads it and returns it #
PROC read integer = ( STRING prompt )INT:
BEGIN
print( ( prompt ) );
INT result;
read( ( result, newline ) );
result
END; # read integer #
# prompts for a string, reads it and returns it #
PROC read string = ( STRING prompt )STRING:
BEGIN
print( ( prompt ) );
STRING result;
read( ( result, newline ) );
result
END; # read string #
# mode to hold a factor and associated text #
MODE FBFACTOR = STRUCT( INT factor, STRING text );
#===============================================================#
# quicksort routine for the factors, from the Algol 68 uicksort #
# task sample #
#---------------------------------------------------------------#
#--- Swap function ---#
PROC swap = (REF[]FBFACTOR array, INT first, INT second) VOID:
( FBFACTOR temp = array[first];
array[first] := array[second];
array[second] := temp
);
#--- Quick sort 3 arg function ---#
PROC quick = (REF[]FBFACTOR array, INT first, INT last) VOID:
( INT smaller := first + 1, larger := last;
FBFACTOR pivot := array[first];
WHILE smaller <= larger DO
WHILE array[smaller] < pivot AND smaller < last DO smaller +:= 1 OD;
WHILE array[larger] > pivot AND larger > first DO larger -:= 1 OD;
IF smaller < larger THEN
swap(array, smaller, larger);
smaller +:= 1;
larger -:= 1
ELSE
smaller +:= 1
FI
OD;
swap(array, first, larger);
IF first < larger -1 THEN quick(array, first, larger-1) FI;
IF last > larger +1 THEN quick(array, larger+1, last) FI
);
# comparison operators #
OP < = ( FBFACTOR a, b )BOOL: factor OF a < factor OF b;
OP > = ( FBFACTOR a, b )BOOL: factor OF a > factor OF b;
#===============================================================#
# get the maximum number to consider #
INT max number = read integer( "Numbers reuired: " );
# number of factors reuired #
INT max factor = 3;
# get the factors and associated words #
[ max factor ]FBFACTOR factors;
FOR i TO max factor DO
factor OF factors[ i ] := read integer( "Factor " + whole( i, 0 ) + ": " );
text OF factors [ i ] := read string( "Text for " + whole( factor OF factors[ i ], 0 ) + ": " )
OD;
# sort the factors into order #
quick( factors, 1, UPB factors );
# play the game #
FOR n TO max number DO
STRING text := "";
FOR factor TO max factor DO
IF n MOD factor OF factors[ factor ] = 0 THEN text +:= text OF factors[ factor ] FI
OD;
IF text = "" THEN
# no words applicable to n, just show the digits #
text := whole( n, 0 )
FI;
print( ( text, newline ) )
OD
END
You may also check:How to resolve the algorithm Sudan function step by step in the Dart programming language
You may also check:How to resolve the algorithm Averages/Mean angle step by step in the Elixir programming language
You may also check:How to resolve the algorithm Statistics/Basic step by step in the Ring programming language
You may also check:How to resolve the algorithm Case-sensitivity of identifiers step by step in the N/t/roff programming language
You may also check:How to resolve the algorithm Substring/Top and tail step by step in the Oforth programming language