FizzBuzz
One of the simplest programming exercises around and yet one that even seasoned programmers fall on.
In short, the task is to write a programme 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’…”
It’s not hard. It really isn’t. It’s far more a logic/workflow problem than it is a programming one. Once you have stated in pseudo-code what you need to do then writing it in real-code isn’t much of a leap.
But for years now, nearly a decade, I’ve not bothered to do it myself. My excuse – such that it is – is that I am not a programmer (yes, I’m learning) I’m a SysAdmin. It is true that as a SysAdmin I write a lot of scripts but I hesitate to call that ‘writing code’ as all it really is is stringing a bunch of commands together. I don’t (really) have to deal with memory management, threading, inter-process communication; none of the stuff that constitutes what I consider to be ‘real’ coding. All that is handled by the software I’m calling to do the job for me.
So, no FizzBuzz. Until Tues 28th August 2012 at around 16:00. It was relatively quiet at work and I had a bit of a time so I thought I’d give it a go. Armed with the one thing I knew how to write – Bash – I set to work.
At 16:32, I sent a text message to my friend Paul:
I just had a spare half hour at work and finally got around to something. It’s not glamorous, it’s probably an abomination unto the lords of Cobol, but here it is… MY FIZZBUZZ!
It had taken me maybe 10 minutes to write, the rest was cleaning up the formatting and commenting it (as well as showing one of my users how to black- and white-list eMail addresses and domains with Postini).
For me the best part of it was that the first time I ran it, it worked!
Here’s what I came up with:
[code lang="bash"]
#! /bin/bash
for n in {1..100}; # Picks our number range
do
if [ $(( $n % 3 )) -eq 0 ] && [ $(( $n % 5 )) -eq 0 ]; # Check if n is divisible by 3 & 5
then echo FizzBuzz # And echo 'FizzBuzz' if it is
elif [ $(( $n % 3 )) -eq 0 ]; # Checks if n is divisible by 3
then echo Fizz # And echo out 'Fizz' if it is
elif [ $(( $n % 5 )) -eq 0 ]; # Checks if n is divisible by 5
then echo Buzz # And echo 'Buzz' if it is
else echo $n # If n isn't divisible by either just echo the number
fi # And
done # Fin
exit 0 # Night Night
[/code]
Yes, I agree, it’s not pretty; it doesn’t have any error checking at all. The first question Paul asked me was:
“…Now the next question in the interview: Can you see any obvious bugs?..”
To which my only answer was:
“…It’s written as a Bash script?..”
Which goes to prove my point about me not being a true coder.
Yes, there are many other ways to write it, even in Bash. But that brings about a couple of questions.
- Does it work
- Without comments, does it make sense
With any script or code I think these are 2 very important questions. You write code to perform a task, that’s it’s purpose, if it can’t do the thing it’s intended to then it is a total waste of electrons. So yes, while my solution may not be the cleverest one-line, no-if-statement, haiku-inspired solution, it did do the most important thing. It worked (did I mention it worked first time?)
Another thing not to loose sight of is the fact you may not maintain the code for its entire lifetime. In fact it’s more likely that you won’t, not by a long shot. So it has to make sense, to be sensible. It’s all very well if you write your one-line, no-if-statement, haiku-inspired solution to a programming problem, but if no-one understands how it does what it does or the reasoning behind it then it’s not going to fare very well going forward. Sometimes of course, the very nature of the problem requires an imaginative solution. So in that case the most important thing are the comments.
Explaining variables, functions and algorithms in natural language is essential. It lets someone in 10 years time understand how and why you solved a problem in a particular way and is one of the most important tools in tracking down bugs. In short, it shows when you had a temporary aberration of mind. Trust me, even YOU will wonder what you were smoking when you go back over your old code. I read scripts I wrote from 10 years ago and am amazed they ever finished running some of them are so inefficiently written!
Be that as it may, FizzBuzz; I have now done it.


hey uncle william!
Hello Sara!
I hope you approve of my web site.