r/tinycode • u/wuffers • Dec 15 '11
FizzBuzz implemented in only 120 characters of Ruby
http://sprunge.us/ZEVW?ruby2
u/peedubyaeff Dec 15 '11
(1..100).each{|x|puts x%15==0?"FizzBuzz":x%3==0?"Fizz":x%5==0?"Buzz":x}72 characters.
Also, although (1..100).each is more characters than 100.times, 100.times goes from 0 to 99 and the only versions of FizzBuzz that I've seen say 'from 1 to 100'.
1
1
u/taw Dec 15 '11
puts (1..100).map{|x|x%15<1?"FizzBuzz":x%3<1?"Fizz":x%5<1?"Buzz":x}Down to 67.
2
1
u/jfredett Dec 15 '11 edited Dec 15 '11
what about...
puts (1..100).map{|x|"#{x%3<1?'fizz':''}#{x%5<1?'buzz':''}"}61 chars?
EDIT ooh- if you don't mind a little bit of an odd output, you can do:
p (1..100).map{|x|"#{x%3<1?'fizz':''}#{x%5<1?'buzz':''}"}for 58 chars.
EDIT deux
Here's a few more chars shaved off
p (1..100).map{|x|(x%3<1?'fizz':'')+(x%5<1?'buzz':'')}(54 chars)
2
Dec 15 '11
None of these are correct solutions; you need to print the value of x if it isn't Fizz or Buzz.
1
u/jfredett Dec 15 '11
swap '' with
xin every place you see it. should work, eg:p (1..100).map{|x|(x%3<1?'fizz':x)+(x%5<1?'buzz':x)}52 chars?
1
Dec 15 '11
can't convert Fixnum into String (TypeError)-- and even if that would get you the string representation of x, it'd go "11, 22, fizz3, 44, 5buzz, ..."1
u/jfredett Dec 15 '11
I'm away from my ruby interp atm. TBH, I've only ever heard it not requiring the number in the off case. I suppose its not exactly high on my priority list... hehe
1
1
u/Rangi42 Jan 01 '12
90 characters in Python:
print"\n".join(["FizzBuzz","Fizz","Buzz",`x`][2*bool(x%3)+bool(x%5)]for x in range(1,101))1
5
u/strabd Dec 15 '11
Why not: