jump to navigation

FizzBuzz :D February 27, 2007

Posted by sdpurtill in Python.

In a recent blog post on Coding Horror entitled Why Can’t Programmers… Program?, they talked about the fact that the majority of so-called programmers are useless when it comes to solving problems. Apparently they give people 10-15 minutes in an interview to solve this simple problem.

After a fair bit of trial and error I’ve discovered that people who struggle to code don’t just struggle on big problems, or even smallish problems (i.e. write a implementation of a linked list). They struggle with tiny problems.

So I set out to develop questions that can identify this kind of developer and came up with a class of questions I call “FizzBuzz Questions” named after a game children often play (or are made to play) in schools in the UK. An example of a Fizz-Buzz question is the 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”.

Most good programmers should be able to write out on paper a program which does this in a under a couple of minutes. Want to know something scary? The majority of comp sci graduates can’t. I’ve also seen self-proclaimed senior programmers take more than 10-15 minutes to write a solution.

I suppose I could have written it in Ruby, ActionScript, PHP, ColdFusion, and JavaScript. But I just learned Python, so I figured I might as well do it in that.

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

Count it. 6 lines. Took 5 minutes. Gotta love Python. I don’t know why I’m bragging, that was a joke.


No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: