Testing FizzBuzz in Parrot

by chromatic

A recent discussion on interviewing programmers (in hopes of finding clueful ones) brought up the FizzBuzz challenge. Can you write a program to print the numbers from one to one hundred, printing also "Fizz" for multiples of three, "Buzz" for multiples of five, and "FizzBuzz" for multiples of three and five?

This ought to take no more than a few minutes for a developer with any proficiency in a language. I decided it would be fun to write it in Parrot's PIR. There's the straightforward procedural way, the array overloading way, an object-oriented way, the coroutine approach, and the generator technique.

I chose the first two, but I also decided to work entirely with test-driven development, even though this is normally the realm of a SpikeSolution--I thought that might be more interesting for everyone.


4 Comments

Tyxod
2007-03-07 07:17:03
The link to Parrotcode is .com where it should be .org
chromatic
2007-03-07 15:26:04
Thanks, Tyxod. I just fixed that and clarified a piece of code!
Mohammad Goldstein
2007-03-07 17:13:53
In PHP, fast as I can make it (and without formatting because of the way comments are handled):

for($i=1;$i<101;++$i)
{
echo((($i%3==0)&&($i%5==0)?'fizzbuzz':(($i%3==0?'fizz':((($i%5==0?'buzz':$i)))))));
}
mr_mischief
2007-03-09 11:08:59
Mohammad,


Forgive me if I'm off base here. I'm not sure if you're just pointing out another solution for comparison or if you've missed the point. The article isn't about conciseness of code. Parrot is meant to be low-level. Perl is a higher-level language which can make use of Parrot as a backend (and will). The development of Perl 6 and Parrot are intertwined and complementary, but the two are quite different languages.


If you really want a short program, Perl 5 easily does that:


print$_%15?$_%5?$_%3?$_:'fizz':'buzz':'fizzbuzz',$/for 1..100


However, clarity suffers a bit and it's possible to write it much more clearly in the same language. Over on Perlmonks.org the gauntlet was thrown to get this program as short as possible, and 63 characters would be far from the winner in that challenge. Clarity suffers even more in many of those solutions, though.


Clarity also suffers in this golfed Ruby program (from contestants of a similar challenge at Ruby-Forum.com):
1.upto(?d){|i|i%3<1&&x=:Fizz;puts i%5<1?"#{x}Buzz":x||i}


Perl and Ruby are much more directly comparable to PHP than is Parrot.