Smallest FizzBuzz program

B

Brian Adkins

This is really bugging me. Someone posted a golf challenge to write a
smallest FizzBuzz program here:

http://golf.shinh.org/p.rb?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
 
J

Jeremy McAnally

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. :)

This is really bugging me. Someone posted a golf challenge to write a
smallest FizzBuzz program here:

http://golf.shinh.org/p.rb?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


--
http://www.jeremymcanally.com/

My free Ruby e-book:
http://www.humblelittlerubybook.com/book/

My blogs:
http://www.mrneighborly.com/
http://www.rubyinpractice.com/
 
R

Rimantas Liubertas

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
 
B

Brian Adkins

Jeremy said:
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.
 
B

Brian Adkins

Rimantas said:
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 :)
 
R

Robert Klemme

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
 
R

Robert Klemme

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
 
B

Brian Adkins

Robert said:
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.
 
V

vsv

This is really bugging me. Someone posted a golf challenge to write a
smallest FizzBuzz program here:

http://golf.shinh.org/p.rb?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 ...
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 :)
 
V

vsv

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 :)
 
B

Brian Adkins

vsv said:
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 :(
 
K

Kyle Schmitt

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.
 
B

Brian Adkins

vsv said:
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 :)

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}
 
M

Martin DeMello

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
 
R

Rick DeNatale

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


1.upto(?d){|i|p ["%sBuzz"%x=[:Fizz][i%3]][i%5]||x||i}

Code golfing is against my religious beliefs however.
 
R

Robert Dober

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


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
 
R

Rick DeNatale

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! ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top