r/tinycode • u/a1phanumeric • Mar 07 '13
FizzBuzz One-Liner (X-Post from /r/ReadableCode)
Ok, I know this is the opposite of "ReadableCode" but I love refactoring little challenges like this (and Project Euler ones). Shame on me, but it's a PHP solution.
for($i=1;$i<=100;$i++)print((($i%15==0)?'FizzBuzz':(($i%5==0)?'Buzz':(($i%3==0)?'Fizz':$i)))."\n");FizzBuzz for those interested.
3
u/kageurufu Mar 07 '13
for(var i=0;i<100;i++)console.log(i%15?i%5?i%3?i:"Buzz":"Fizz":"FizzBuzz")reverse your logic to cut out the ==0
also, the extra parenthesis arent needed.
3
u/TJSomething Mar 08 '13 edited Mar 08 '13
I think I got that a bit better refined, if we ignore PHP notices:
while($i++<100)echo($i%3?"":Fizz).($i%5?"":Buzz)?:$i,"\n";Today I learned that ?: was a thing.
3
u/sirin3 Mar 08 '13 edited Mar 08 '13
For contrast, here is my FizzBuzz with over 2000 words
Took me three days to write, but it is not beautiful?
And it is even tiny code, there are no fillers, every word is necessary (well, except some in the first line) and I could not find a shorter version
2
u/OrangeredStilton Mar 08 '13
I instigated a little golfing game among my friends recently, to see who could get the smallest PHP Fizzbuzz. My contribution:
<?while($i<100)echo$s=(++$i%3?'':Fizz).($i%5?'':Buzz)?:$i;Someone else did better, but I don't have his copy to hand.
3
u/[deleted] Mar 07 '13 edited Mar 07 '13
Shortest Haskell one I could come up with.