Menu Categories Author

Nubis Novem

Consulting On Cloud Nine

What’s the Fizz Buzz: A fully functional code example in Perl

[APOSTLES]
What’s the buzz, tell me what’s a-happening
What’s the buzz, tell me what’s a-happening

“What’s the Buzz”, from “Jesus Christ Superstar”, The Original Motion Picture Soundtrack (1973)

Here is why should you want to know about so called “Fizz Buzz”: it is mystifying!.. No, not really, at least not if you know how to program. Simply put that is one of most common requests to write a sample code while interviewing for a programming job. As quoted on c2.com the “Fizz Buzz” request might be formulated as following:

“Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.”

Of course, this should result in a few lines of codes using one of commonly used programming languages today. Perl5 is hardly one of popular ones. That is why it was interesting enough for us to spend a few moments to think and offer you the following live recipe.

First, we needed to check whether a number is divisible by 3 and by 5. In Perl that would be achieved by “modulo” operator %, which calculates a reminder of division by a certain number. Thus, 15 % 5 will be 0 and 16 % 5 will be 1. So, 0 or not 0, that would be binary logic to answer the question whether a number is a multiple of three or five. That can be implemented as a method:


sub FizzBuzzed {
   my $DivisbleBy3 = ( $_ % 3 ) ;
   my $DivisbleBy5 = ( $_ % 5 ) ;
   return 'FizzBuzz' if $DivisbleBy3 && $DivisbleBy5;
   return 'Fizz' if $DivisbleBy3;
   return 'Buzz' if $DivisbleBy5;
   return $_;
}

However this was not as elegant as Perl code should look like, at least not in our bible. Let us try to think what a functional programmer would do. Let us reduce the number of operations and checks with conditional operator “?”. Then we would be able to produce ‘Fizz’ by simply evaluating the incoming value as ( $_ % 3 ) ? ” : ‘Fizz’ and ( $_ % 5 ) ? ” : ‘Buzz’ or better yet let us put them into a one liner:


sub FizzBuzzed {
   ( $_ % 3 ? '' : 'Fizz' ) . ( $_ % 5 ? '' : 'Buzz' ) ;
}

We did not need to use return this time, as the last evaluated value would be returned as the result of the Perl method. However, we seemed to forget to handle the case when the numeric value was not multiple of 3 or 5, and had to return the original value itself. That would be done using conditional operator || which will return value passed to it only when the expression prepending it would be evaluated as 0 or empty or undefined and the code ended up looking this way:


sub FizzBuzzed {
   ( $_ % 3 ? '' : 'Fizz' ) . ( $_ % 5 ? '' : 'Buzz' ) || $_ ;
}

Now it was almost perfect; then for better output formatting we had to add a new line character to the output line we formed:


sub FizzBuzzed {
   ( ( $_ % 3 ? '' : 'Fizz' ) . ( $_ % 5 ? '' : 'Buzz' ) || $_ ) . "\n";
}

Then we wanted to produce series of numbers from 1 to 100. That is achieved by (1 .. 100) expression in Perl using the range operator ‘..’. Finally, we needed to use map to iterate through the series and call our “filtering” method for each value of the array and print it to the console output:


print map { FizzBuzzed } (1 .. 100);

That would be all, folks. Test implementation might be left for you as home work. Here is the full “Fizz Buzz” sample of code as we seen it to fit:


#!perl -w

sub FizzBuzzed {
   ( ( $_ % 3 ? '' : 'Fizz' ) . ( $_ % 5 ? '' : 'Buzz' ) || $_ ) . "\n";
}

print map { FizzBuzzed } (1 .. 100);

It worked for us with the print out:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz
Fizz
52
53
Fizz
Buzz
56
Fizz
58
59
FizzBuzz
61
62
Fizz
64
Buzz
Fizz
67
68
Fizz
Buzz
71
Fizz
73
74
FizzBuzz
76
77
Fizz
79
Buzz
Fizz
82
83
Fizz
Buzz
86
Fizz
88
89
FizzBuzz
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz
Buzz

Leave a Reply

Your email address will not be published. Required fields are marked *