| « Ouch | The BoyGirl paradox » |
Thu, Oct 20, 2011
![[Icon]](rsc/img/chain_link.gif)
I was reading this article about self-proclaimed programmers who can't actually program - something I've had some experience of myself.
I quite liked the idea of the FizzBuzz challenge:
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".
It's dead easy to do this sloppily: Test if it divides by three and five, else test if it divides by three, else test if it divides by five, else print the number. But it's slightly more difficult to do it neatly/elegantly.
So I gave myself five minutes to try and do it 'properly', and I came up with this bit of perl:
for my $i (1..100){
my $print;
$print .= "Fizz" unless $i % 3;
$print .= "Buzz" unless $i % 5;
$print ||= $i;
print "$print\n";
}
Anyone know of any other interesting ways to do it? :)
![[Links]](http://geekblog.oneandoneis2.org/skins/112/rsc/img/chain_link.gif)
I'm in the Perl newsletter again. I should try and write about some other language...
21/05/12
Facebook Syndication Error
22/05/12
![]()
I last listened to:
Johann Pachelbel - Canon in D major
Most recent photo:
js.js