Yenya's World

Mon, 28 Jan 2008

FizzBuzz

Pete Zaitcev mentions an interesting programming challenge (read on only if you have several minutes of free time, so that you can try it yourself):

Write on paper, as fast as you can, a program, which prints out the numbers from 1 to 100, but instead of numbers divisible by three it prints "Fizz", instead of numbers divisible by five, it prints "Buzz", and instead of numbers divisible by both 3 and 5 it prints "FizzBuzz".

Pete mentions that even some comp.sci graduates cannot solve this problem. The difficulty of course varies with the programming language used. My own solution in C took me about 3.5 minutes, and the short part of this time I have not been busy writing I spent thinking about a clever way of doing it using conditional expressions in one command, instead of writing the obvious solution.

Anyway, here is the original FizzBuzz article, and there is also a Golf challenge: I am at 60 characters now (in Perl, of course), but they do not accept the command-line switches, so the actual solution has 63 chars instead.

Section: /computers (RSS feed) | Permanent link | 3 writebacks

3 replies for this story:

adelton wrote:

On paper about 30 seconds in Perl. In that Golf, 58 strokes after trying for about 20 minutes. That probably makes Perl my favorite language.

Milan Zamazal wrote:

I wonder when people will finally manage to understand the read/write essence of computer programming. What I miss in the challenge is to put the final code in front of other people and measure how long it takes them to understand what the code does and modify it for a different behavior. *Those* would be interesting results from my point of view.

Yenya wrote: Re: Milan Zamazal

I suppose you are talking about Golf. Well, nobody sane would try to write an information system or an OS kernel Golf style. However, Golf _is_ an important mental exercise, helping both thinking out unusual way of doing things (like factorizing numbers using the s/// command), and getting familiar with not-often-used parts of the language in question. Both writing _and_ reading Golf programs are enlightening (as any other non-trivial mental activity, such as learning Japanese :-). As for the challenge you suggest, there is no simple way of exactly measure the results, and because peoples opinion tend to differ on things like code formating, VariableNaming, etc., the code readability is pretty local in a given project with a given language, coding style, and even code structuring rules.

Reply to this story:

 
Name:
URL/Email: [http://... or mailto:you@wherever] (optional)
Title: (optional)
Comments:
Key image: key image (valid for an hour only)
Key value: (to verify you are not a bot)

About:

Yenya's World: Linux and beyond - Yenya's blog.

Links:

RSS feed

Jan "Yenya" Kasprzak

The main page of this blog

Categories:

Archive:

Blog roll:

alphabetically :-)