I mean, other than “The Adventures of Accordian Guy in the Twenty-First Century“?
Yes, yes he does.
I ran across this on the Y Combinator Twitter yesterday, and thought I’d give FizzBuzz a shot. I’d estimate it took me just under 30 minutes to get the code you see here, which I believe “works”. Part of that time was taken up with assisting one of my cow orkers with a problem, though. An embarrassingly large chunk of that time was taken up by my having to look up the Perl syntax for “for”, “if”, and the modulo operator. I’m a bit rusty; the last time I wrote substantial Perl code was about a year ago (a Perl script that parses CSV data from a file and imports it into a SQL database).
Anyway, code:
#!/usr/bin/perl
for ($index = 1; $index < 101; $index++)
{
$div_by_3 = 0;
$div_by_5 = 0;
if ($index % 3 == 0) {
$div_by_3 = 1;
}
if ($index % 5 == 0) {
$div_by_5 = 1;
}
if ($div_by_3 == 1 && $div_by_5 == 1 ) {
printf "FizzBuzz\n";
} else {
if ($div_by_3 == 1) {
printf "Fizz\n";
} else {
if ($div_by_5 ==1){
printf "Buzz\n";
} else {
printf "$index\n";
}
}
}
}
As always, when I put stuff like this up, I welcome criticism or comment on how I could have done it better (or, in this case, “right” if I did it wrong). The way I see it, I can’t get any better if I don’t solicit and accept criticism.
(Followup from deVilla here.)
Edited to add: I was going to upload a Python version that I wrote in (about) 20 minutes (I think). I keep planning to sit down and learn Python, but then somebody calls and wants to go riding bikes or whatever…anyway, I couldn’t paste that here and have it come out the way I wanted to, so I’ve uploaded it here. (I had to change the extension from “.py” to “.txt” because WordPress didn’t like “.py”.)