How to resolve the algorithm General FizzBuzz step by step in the Batch File programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm General FizzBuzz step by step in the Batch File 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 Batch File programming language

Source code in the batch programming language

@echo off
rem input range
set /p "range=> "

rem input data (no error-checking)
set "data_ctr=0"
:input_loop
set "data="
set /p "data=> "
if "%data%" equ "" goto count
rem parsing data into 1-based pseudo-array
set /a "data_ctr+=1"
for /f "tokens=1-2 delims= " %%D in ("%data%") do (
   set "facto%data_ctr%=%%D"
   set "print%data_ctr%=%%E"
)
goto input_loop

rem perform fizzbuzz now
:count
setlocal enabledelayedexpansion
for /l %%C in (1,1,%range%) do (
   set "out="
   for /l %%X in (1,1,%data_ctr%) do (
      set /a "mod=%%C %% facto%%X"
      if !mod! equ 0 set "out=!out!!print%%X!"
   )
   if not defined out (echo %%C) else (echo !out!)
)
pause
exit /b 0

  

You may also check:How to resolve the algorithm Longest increasing subsequence step by step in the Julia programming language
You may also check:How to resolve the algorithm Nth root step by step in the F# programming language
You may also check:How to resolve the algorithm Continued fraction step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Synchronous concurrency step by step in the Elixir programming language
You may also check:How to resolve the algorithm Zeckendorf number representation step by step in the Scheme programming language