M
Michele Dondi
David Flanagan in his book "Java examples in a nutshell" mentions a
game called "fizz buzz"[1] in which two players must enumerate numbers
from 1 to 100 in turns saying 'fizz' if the number is divisible by 5,
'buzz' if it is divisible by 7 and 'fizzbuzz' if it is divisible by
both.
Then he gives a Java program running a version of the game in which of
course there aren't two players but numbers or 'fizz's and 'buzz's are
simply printed to STDOUT. So it was natural (well, for me!) to think
about the same thing[2] in Perl...
So, why not thinking about the shortest way to do it, too?!? Well, my
first idea was something along the lines of
#!/usr/bin/perl -l
print[$_,fizz,buzz,fizzbuzz]->[!($_%7)*2+!($_%5)]for 1..100
that indeed is interesting on its own IMHO, but I was really bothered
by that 'fizzbuzz' thing and OTOH any trick to avoid inserting it as a
literal seemed more expensive.
Eventually this is the best I can do:
#!/usr/bin/perl
print+($_%5?'':fizz).($_%7?'':buzz)||$_,$/for 1..100
(-l is more expensive as of the rules I know for perlgolf, see e.g.
<http://perlgolf.terje.org>).
Anything "better"?
[1] Source code for the book is available online so I guess one should
be able to find easily the program mentioned in this paragraph, if
interested.
[2] Actually the original program prints its output as a space
separated list, but it seems *to me* more perl(golf)-customary to ask
for a newline-separated list instead.
Michele
game called "fizz buzz"[1] in which two players must enumerate numbers
from 1 to 100 in turns saying 'fizz' if the number is divisible by 5,
'buzz' if it is divisible by 7 and 'fizzbuzz' if it is divisible by
both.
Then he gives a Java program running a version of the game in which of
course there aren't two players but numbers or 'fizz's and 'buzz's are
simply printed to STDOUT. So it was natural (well, for me!) to think
about the same thing[2] in Perl...
So, why not thinking about the shortest way to do it, too?!? Well, my
first idea was something along the lines of
#!/usr/bin/perl -l
print[$_,fizz,buzz,fizzbuzz]->[!($_%7)*2+!($_%5)]for 1..100
that indeed is interesting on its own IMHO, but I was really bothered
by that 'fizzbuzz' thing and OTOH any trick to avoid inserting it as a
literal seemed more expensive.
Eventually this is the best I can do:
#!/usr/bin/perl
print+($_%5?'':fizz).($_%7?'':buzz)||$_,$/for 1..100
(-l is more expensive as of the rules I know for perlgolf, see e.g.
<http://perlgolf.terje.org>).
Anything "better"?
[1] Source code for the book is available online so I guess one should
be able to find easily the program mentioned in this paragraph, if
interested.
[2] Actually the original program prints its output as a space
separated list, but it seems *to me* more perl(golf)-customary to ask
for a newline-separated list instead.
Michele