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

Published on 12 May 2024 09:40 PM

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

Source code in the perl programming language

#!bin/usr/perl
use 5.020;
use strict;
use warnings;

#Get a max number from the user
say("Please enter the maximum possible multiple. ");
my $max = <STDIN>;

#Get the factors from the user
my @factors = ();
my $buffer;
say("Now enter the first factor and its associated word. Ex: 3 Fizz ");
chomp($buffer = <STDIN>);
push @factors, $buffer;
say("Now enter the second factor and its associated word. Ex: 5 Buzz ");
chomp($buffer = <STDIN>);
push @factors, $buffer;
say("Now enter the third factor and its associated word. Ex: 7 Baxx ");
chomp($buffer = <STDIN>);
push @factors, $buffer;

#Counting from 1 to max
for(my $i = 1; $i <= $max; $i++)
{
    #Create a secondary buffer as well as set the original buffer to the current index
    my $oBuffer;
    $buffer = $i;
    #Run through each element in our array
    foreach my $element (@factors)
    {
        #Look for white space
        $element =~ /\s/;
        #If the int is a factor of max, append it to oBuffer as a string to be printed
        if($i % substr($element, 0, @-) == 0)
        {
            $oBuffer = $oBuffer . substr($element, @+ + 1, length($element));
            #This is essentially setting a flag saying that at least one element is a factor
            $buffer = "";
        }
    }
    #If there are any factors for that number, print their words. If not, print the number.
    if(length($buffer) > 0)
    {
        print($buffer . "\n");
    }
    else
    {
        print($oBuffer . "\n");
    }
}


  

You may also check:How to resolve the algorithm A+B step by step in the Nemerle programming language
You may also check:How to resolve the algorithm Mastermind step by step in the Perl programming language
You may also check:How to resolve the algorithm Roots of a function step by step in the Clojure programming language
You may also check:How to resolve the algorithm Multifactorial step by step in the Java programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the Emacs Lisp programming language