Do you know where your variables are? PerlMonks

### Re: Golf Challenge: FizzBuzz

by blazar (Canon)
 on Mar 03, 2007 at 15:13 UTC ( #603063=note: print w/replies, xml ) Need Help??

in reply to Golf Challenge: FizzBuzz

In stumbling across this disturbing writeup, the author claims that not only can the majority of comp-sci majors not solve the 'fizzbuzz' problem, but he's also seen "senior" programmers take 10 or 15 minutes to solve it:
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”.

I was quite sure I had already seen this thing. I thought it must have surely been in clpmisc. So I searched there and... much to my surprise it was actually a thread started by me! (available from Google Groups, too.)

Actually the requirements there were slightly different: to print "'fizz' if the number is divisible by 5, 'buzz' if it is divisible by 7 and 'fizzbuzz' if it is divisible by both." (Dor all numbers from 1 up to 100.) The best solution there was given by Jay Tilton:

```print+fizz x/0|5\$/.buzz x!(\$_%7)||\$_,\$/for 1..100

```print+Fizz x!(\$_%3).Buzz x/0|5\$/||\$_,\$/for 1..100

And it's 49. But Sidhekin's solution, if amended with a newline:

```print+(Fizz)[\$_%3].(Buzz)[\$_%5]||\$_,\$/for 1..100

scores 48, and wins!

Replies are listed 'Best First'.
Re^2: Golf Challenge: FizzBuzz
by shmem (Chancellor) on Mar 04, 2007 at 22:11 UTC
```print+(Fizz)[\$_%3].(Buzz)[\$_%5]||\$_,\$/for 1..100
scores 48, and wins!
Hmm? That's the solution I posted to FizzBuzz before this thread even started...

--shmem

```_(\$_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
/\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.(\$e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;\$e.e && print}```

Well done, then, I wanted to write "which is also shmem's solution", but I didn't because in this thread the other one appeared first. I won't update my node to acknowledge that you came first, since... well... these further followups do. Compliments again!

```#2345678901234567890123456789012345678901234567
warn+(Fizz)[\$_%3].(Buzz)[\$_%5]||\$_,\$/for 1..100
also works with one fewer characters.
```                #2345678901234567890123456789012345678901234
perl -M5.01 -e 'say+(Fizz)[\$_%3].(Buzz)[\$_%5]||\$_,for 1..100'
But this isn't really golf since you need to add -M5.01 to the calling statement.

As you can see from the date of the post to which you answered, it was written previous to the release of perl5.10 - there is no 'say' in 5.8.8...

Create A New User
Node Status?
node history
Node Type: note [id://603063]
help
Chatterbox?
 [GotToBTru]: ugh. Thanks Corion [shmem]: meh. setting up a SIG{__DIE__} before loading Net::SSLeay still triggers Can't locate object method "tid" via package "threads" at /usr/share/perl/5. 20/XSLoader.pm

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (11)
As of 2017-07-26 15:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
I came, I saw, I ...

Results (396 votes). Check out past polls.