IAmTheRockstar

Yes. Yes I am.
You are accessing the archives. Commenting has been disabled, and archive posts are likely to be horribly out of date and, in some cases, rendered oddly because the styles from archive posts are no longer being maintained.
March 13, 2007

The Infamous FizzBuzz

When I get bored at work, I read articles about programming techiniques. I've found that instead of reading books about design patterns, I've found it much more beneficial to read a blog post about a code snippet, showing the code snippet and the "Why" for the snippet. So naturally, I find myself in the Programming Section of Reddit on a pretty frequent basis. Anyway, I found myself folling an article about a unit test for FizzBuzz. Not knowing what FizzBuzz was, I thought I read up on it. You can find the original article here.

While asking about what FizzBuzz, the guys in the office explained it to me. The basic idea is that FizzBuzz is a programming question often asked in interviews, and apparently, only 20% of developers can solve it (I can't vouch for that stastic. It was thrown out by someone here at work). Anyway, the basic idea is to create an algorithm from 1 to 100. If the current number is divisible by 3, print "Fizz" to the screen. If it's divisible by 5, print "Buzz" to the screen. If it's divisible by both, then print "FizzBuzz" to the screen. Otherwise, just print the number.

I can probably do this in at least 4 programming languages, 5 if you include shell scripting. However, I hadn't heard of this, so I took a whack at it. My knee-jerk language of choice is python. So I instantly wanted to prove to myself that I could figure out this simple problem, if only to know that I myself can do it.

Here was my first attempt (formatted for easier reading...er, easier than normal Python reading):

#!/usr/bin/env python
for i in range (1, 100):
  if ( i % 3 == 0 ):
    print 'Fizz!'
  elif ( i % 5 == 0 ):
    print 'Buzz!'
  elif ( i % 3 == 0 and i % 5 == 0 ):
    print 'FizzBuzz!'
  else:
    print i

This didn't really work out though, for one reason only, but it was a testament to how well I test a program. With the structure like this, "FizzBuzz" will never output, because the first check will evaluate True, so it will only print "Fizz" even though it is divisible by both 3 and 5. So I changed it around...

for i in range (1, 100):
  if ( i % 3 == 0 and i % 5 == 0 ):
    print 'FizzBuzz!'
  elif ( i % 3 == 0):
    print 'Fizz!'
  elif ( i % 5 == 0 ):
    print 'Buzz!'
  else:
    print i

Voila! It worked! I don't think I could have done this with any language when I first started working here, so I guess I'm not that much better than those developers who can't do it, but, honestly, I was hired as a charity case, and I've tried every day to make sure my employer gets paid off for their risk taking.

No comments have been posted yet.
Comments are now closed an archived posts.

All opinions expressed here constitute my personal opinion, and do not necessarily represent the opinion of any other organization or person, including, but not limited to, my fellow employees, my employer, its clients or their agents.