This is really bugging me. Someone posted a golf challenge to write a
smallest FizzBuzz program here:
anarchy golf - FizzBuzz (although the site was down when I
checked it a few minutes ago)
Basically, the challenge is to write the smallest Ruby program that will
print the numbers from 1 to 100 except:
- substitute Fizz for numbers that are multiples of 3
- substitute Buzz for numbers that are multiples of 5
- substitute FizzBuzz for numbers that are multiples of both 3 and 5
Also see:
http://weblog.raganwald.com/2007/01/dont-overthink-fizzbuzz.html
The winning entry is at 56 bytes and I can’t get below 65 bytes with the
following:
1.upto(100){|i|puts"FizzBuzz#{i}"[i%3<1?0:i%5<1?4:8,i%15<1?8:4]}
I think if the substring indices could be computed mathematically
instead of logically, it might work, but it’s possible an entirely new
approach is necessary.
It works correctly, so to see acceptable output, just run it.
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
…
Can any Ruby guru out there get it down to 56 bytes?
Brian
1.upto(100){|i|p"FizzBuzz#{i}"[i%3<1?0:i%5<1?4:8,i%15<1?8:4]}
Shaves off 3 bytes.
On 3/2/07, Brian A. [email protected] wrote:
- substitute FizzBuzz for numbers that are multiples of both 3 and 5
approach is necessary.
8
Brian
–
http://www.jeremymcanally.com/
My free Ruby e-book:
http://www.humblelittlerubybook.com/book/
My blogs:
http://www.rubyinpractice.com/
1.upto(100){|i|p"FizzBuzz#{i}"[i%3<1?0:i%5<1?4:8,i%15<1?8:4]}
Shaves off 3 bytes.
But it messes with the output a bit…
You can save one byte doing this: 1.upto(?d)
Regards,
Rimantas
On 02.03.2007 17:34, Jeremy McAnally wrote:
1.upto(100){|i|p"FizzBuzz#{i}"[i%3<1?0:i%5<1?4:8,i%15<1?8:4]}
Shaves off 3 bytes.
100.times{|i|p"FizzBuzz#{i}"[i%3<1?0:i%5<1?4:8,i%15<1?8:4]}
another two.
robert
Rimantas L. wrote:
1.upto(100){|i|p"FizzBuzz#{i}"[i%3<1?0:i%5<1?4:8,i%15<1?8:4]}
Shaves off 3 bytes.
But it messes with the output a bit…
You can save one byte doing this: 1.upto(?d)
Awesome, ASCII value of letter d ! Only 8 more bytes to shave off
Jeremy McAnally wrote:
1.upto(100){|i|p"FizzBuzz#{i}"[i%3<1?0:i%5<1?4:8,i%15<1?8:4]}
Shaves off 3 bytes.
Thanks, but that breaks the output by adding “”, so it won’t do.
Robert K. wrote:
On 02.03.2007 17:34, Jeremy McAnally wrote:
1.upto(100){|i|p"FizzBuzz#{i}"[i%3<1?0:i%5<1?4:8,i%15<1?8:4]}
Shaves off 3 bytes.
100.times{|i|p"FizzBuzz#{i}"[i%3<1?0:i%5<1?4:8,i%15<1?8:4]}
another two.
Nope. 100.times is not equivalent to 1.upto(100) - off by one error.
On 02.03.2007 18:00, Rimantas L. wrote:
1.upto(100){|i|p"FizzBuzz#{i}"[i%3<1?0:i%5<1?4:8,i%15<1?8:4]}
Shaves off 3 bytes.
But it messes with the output a bit…
You can save one byte doing this: 1.upto(?d)
?d.times{|i|puts"FizzBuzz#{i}"[i%3<1?0:i%5<1?4:8,i%15<1?8:4]}
Hm…
robert
On Mar 2, 2007, at 12:15 PM, Robert K. wrote:
?d.times{|i|puts"FizzBuzz#{i}"[i%3<1?0:i%5<1?4:8,i%15<1?8:4]}
This approach does 0…99 though…
-Mat
On Mar 2, 11:06 am, Brian A. [email protected] wrote:
- substitute FizzBuzz for numbers that are multiples of both 3 and 5
…
Can any Ruby guru out there get it down to 56 bytes?
55 bytes:
1.upto(?d){|i,x|i%3<1&&x=:Fizz;puts i%5<1?“#{x}Buzz”:i}
Replace ?d with 100 if you want 56 bytes
On 02.03.2007 18:22, Mat S. wrote:
On Mar 2, 2007, at 12:15 PM, Robert K. wrote:
?d.times{|i|puts"FizzBuzz#{i}"[i%3<1?0:i%5<1?4:8,i%15<1?8:4]}
This approach does 0…99 though…
Darn. Golf is just not my sport - never was.
robert
Ummm… funny… I see 9 and 12 in the output when I ran this
1.upto(?d){|i,x| i%3<1&&x=:Fizz;puts (i%3<1||i%5<1)?"#{x}Buzz":i} is
back up to 65. Shame.
vsv wrote:
- substitute Buzz for numbers that are multiples of 5
- substitute FizzBuzz for numbers that are multiples of both 3 and 5
…
Can any Ruby guru out there get it down to 56 bytes?
55 bytes:
1.upto(?d){|i,x|i%3<1&&x=:Fizz;puts i%5<1?"#{x}Buzz":i}
Replace ?d with 100 if you want 56 bytes
Interesting. But it doesn’t produce proper output
On Mar 2, 2:21 pm, “Kyle S.” [email protected] wrote:
Ummm… funny… I see 9 and 12 in the output when I ran this
it is my fault, last second optimization is always wrong
(my lunch is too short and I can’t use ruby in the office ),
best I can get if 58 chars:
1.upto(?d){|i,x|i%3<1&&x=:Fizz;puts i%5<1?“#{x}Buzz”:x||i}
hope it works OK
On 3/3/07, Brian A. [email protected] wrote:
Nice work. I removed ,x from |i,x| on a whim and it still works - 57
bytes! (you have to count file size which as an EOF char)
1.upto(?d){|i|i%3<1&&x=:Fizz;puts i%5<1?“#{x}Buzz”:x||i}
Slight variant, though sadly the same length:
1.upto(?d){|i|puts [“%sBuzz”%x=[:Fizz][i%3]][i%5]||x||i}
martin
vsv wrote:
Nice work. I removed ,x from |i,x| on a whim and it still works - 57
bytes! (you have to count file size which as an EOF char)
1.upto(?d){|i|i%3<1&&x=:Fizz;puts i%5<1?"#{x}Buzz":x||i}
On 3/2/07, Martin DeMello [email protected] wrote:
martin
1.upto(?d){|i|p [“%sBuzz”%x=[:Fizz][i%3]][i%5]||x||i}
Code golfing is against my religious beliefs however.
–
Rick DeNatale
My blog on Ruby
http://talklikeaduck.denhaven2.com/
On 3/3/07, Rick DeNatale [email protected] wrote:
1.upto(?d){|i|puts [“%sBuzz”%x=[:Fizz][i%3]][i%5]||x||i}
martin
1.upto(?d){|i|p [“%sBuzz”%x=[:Fizz][i%3]][i%5]||x||i}
Code golfing is against my religious beliefs however.
good decision 'cause you are cheating, the output format of p is
not what is required
Robert
On 3/2/07, Robert D. [email protected] wrote:
On 3/3/07, Rick DeNatale [email protected] wrote:
1.upto(?d){|i|p [“%sBuzz”%x=[:Fizz][i%3]][i%5]||x||i}
Code golfing is against my religious beliefs however.
good decision 'cause you are cheating, the output format of p is
not what is required
Robert
And here I could have sworn that I’d actually tried that very code in
irb.
That’s why I’m against code golfing, rots the brain!
–
Rick DeNatale
My blog on Ruby
http://talklikeaduck.denhaven2.com/