[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Smallest FizzBuzz program

Brian Adkins

3/2/2007 4:07:00 PM

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

http://golf.shinh.org/p.r... (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-fiz...

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
32 Answers

Jeremy McAnally

3/2/2007 4:34:00 PM

0

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 Adkins <lojicdotcomNOSPAM@gmail.com> wrote:
> This is really bugging me. Someone posted a golf challenge to write a
> smallest FizzBuzz program here:
>
> http://golf.shinh.org/p.r... (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-fiz...
>
> 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.jeremymca...

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

My blogs:
http://www.mrneigh...
http://www.rubyinpra...

Rimantas Liubertas

3/2/2007 5:01:00 PM

0

> 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
--
http://rim...

Brian Adkins

3/2/2007 5:05:00 PM

0

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.

Brian Adkins

3/2/2007 5:10:00 PM

0

Rimantas Liubertas 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 :)

> Regards,
> Rimantas
> --
> http://rim...
>

Robert Klemme

3/2/2007 5:11:00 PM

0

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

Robert Klemme

3/2/2007 5:13:00 PM

0

On 02.03.2007 18:00, Rimantas Liubertas 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

Brian Adkins

3/2/2007 5:14:00 PM

0

Robert Klemme 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.

>
> robert

Mat Schaffer

3/2/2007 5:23:00 PM

0


On Mar 2, 2007, at 12:15 PM, Robert Klemme 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

Robert Klemme

3/2/2007 5:38:00 PM

0

On 02.03.2007 18:22, Mat Schaffer wrote:
>
> On Mar 2, 2007, at 12:15 PM, Robert Klemme 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

Sergey Volkov

3/2/2007 7:14:00 PM

0

On Mar 2, 11:06 am, Brian Adkins <lojicdotcomNOS...@gmail.com> wrote:
> This is really bugging me. Someone posted a golf challenge to write a
> smallest FizzBuzz program here:
>
> http://golf.shinh.org/p.r... (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 :)