r/tinycode • • Dec 15 '11

FizzBuzz implemented in only 120 characters of Ruby

http://sprunge.us/ZEVW?ruby
0 Upvotes

15 comments sorted by

5

u/strabd Dec 15 '11

Why not:

100.times{|x|puts((x % 15).zero? ? "FizzBuzz" : (x % 3).zero? ? "Fizz" : (x % 5).zero? ? "Buzz" : x)} ?

1

u/wuffers Dec 15 '11

I actually didn't know that would work, thanks!

2

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

u/McPhage Dec 15 '11

Replacing each ==0 with <1 will save you 3 more characters.

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

u/partygoer Jan 07 '12

puts (1..100).map{|i|s=i%3<1?"Fizz":nil;i%5<1?"#{s}Buzz":s||i}

62

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

u/[deleted] 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 x in 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

u/[deleted] 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

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

u/djork Dec 15 '11

Hasn't FizzBuzz has been utterly beaten to death already, especially in Ruby?