Forum: Ruby Smallest FizzBuzz program

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F3121a9b0999cefc9bf2d9a68439d258?d=identicon&s=25 Brian Adkins (Guest)
on 2007-03-02 17:10
(Received via mailing list)
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...

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
6d3c187a8b3ef53b08e3e7e8572c4fea?d=identicon&s=25 Jeremy McAnally (Guest)
on 2007-03-02 17:34
(Received via mailing list)
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:
> * 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.mrneighborly.com/
http://www.rubyinpractice.com/
Ec5a599777854c540fd102ef4691fe10?d=identicon&s=25 Rimantas Liubertas (Guest)
on 2007-03-02 18:01
(Received via mailing list)
> 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
F3121a9b0999cefc9bf2d9a68439d258?d=identicon&s=25 Brian Adkins (Guest)
on 2007-03-02 18:10
(Received via mailing list)
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.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-03-02 18:15
(Received via mailing list)
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
F3121a9b0999cefc9bf2d9a68439d258?d=identicon&s=25 Brian Adkins (Guest)
on 2007-03-02 18:16
(Received via mailing list)
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 :)
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-03-02 18:17
(Received via mailing list)
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
F3121a9b0999cefc9bf2d9a68439d258?d=identicon&s=25 Brian Adkins (Guest)
on 2007-03-02 18:18
(Received via mailing list)
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.
Fc784eadb3b54531fdc3d2053db6f83f?d=identicon&s=25 Mat Schaffer (Guest)
on 2007-03-02 18:22
(Received via mailing list)
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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-03-02 18:40
(Received via mailing list)
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
Ced5fff44ff8929fc974012ea108b284?d=identicon&s=25 Sergey Volkov (rf-vsv)
on 2007-03-02 20:16
(Received via mailing list)
On Mar 2, 11:06 am, Brian Adkins <lojicdotcomNOS...@gmail.com> 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 :)
9dec3df8319c613f6f4f14a27da0fdb4?d=identicon&s=25 Kyle Schmitt (Guest)
on 2007-03-02 20:22
(Received via mailing list)
Ummm..... funny.... I see 9 and 12 in the output when I ran this :)
9dec3df8319c613f6f4f14a27da0fdb4?d=identicon&s=25 Kyle Schmitt (Guest)
on 2007-03-02 20:30
(Received via mailing list)
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.
F3121a9b0999cefc9bf2d9a68439d258?d=identicon&s=25 Brian Adkins (Guest)
on 2007-03-02 20:31
(Received via mailing list)
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 :(
Ced5fff44ff8929fc974012ea108b284?d=identicon&s=25 Sergey Volkov (rf-vsv)
on 2007-03-02 20:32
(Received via mailing list)
On Mar 2, 2:21 pm, "Kyle Schmitt" <kyleaschm...@gmail.com> 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 :)
F3121a9b0999cefc9bf2d9a68439d258?d=identicon&s=25 Brian Adkins (Guest)
on 2007-03-02 20:40
(Received via mailing list)
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}
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (Guest)
on 2007-03-02 21:49
(Received via mailing list)
On 3/3/07, Brian Adkins <lojicdotcomNOSPAM@gmail.com> 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
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2007-03-03 01:06
(Received via mailing list)
On 3/2/07, Martin DeMello <martindemello@gmail.com> 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/
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-03-03 01:21
(Received via mailing list)
On 3/3/07, Rick DeNatale <rick.denatale@gmail.com> 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
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2007-03-03 01:30
(Received via mailing list)
On 3/2/07, Robert Dober <robert.dober@gmail.com> wrote:
> On 3/3/07, Rick DeNatale <rick.denatale@gmail.com> 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/
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-03-03 01:40
(Received via mailing list)
On 3/3/07, Rick DeNatale <rick.denatale@gmail.com> wrote:
> 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! ;-)
That's why I love it, nothing to lose ;)
But this one got on my nervers now, good night everyone!
F3121a9b0999cefc9bf2d9a68439d258?d=identicon&s=25 Brian Adkins (Guest)
on 2007-03-03 04:16
(Received via mailing list)
Brian Adkins wrote:
> 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)

I submitted the resulting program to the site (which is back up now)
under 'comp.lang.ruby' since multiple people contributed. Comes in at 57
bytes - only 1 byte longer than the best. So apparently the site does
count the EOF byte.

1.upto(?d){|i|i%3<1&&x=:Fizz;puts i%5<1?"#{x}Buzz":x||i}

http://golf.shinh.org/p.rb?FizzBuzz

Interestingly, the following Ruby program (with a little help from sed
that my friend passed along) comes in at exactly 56 bytes, but the test
site rightfully failed it due to the attempt to shell the command:

puts `seq 100|sed -e'5~5s/.*/Buzz/;3~3s/^[0-9]*/Fizz/'`
C276b65ba29e2aedbfd7f4806ba56c4e?d=identicon&s=25 Guillaume Nargeot (Guest)
on 2007-03-05 06:50
(Received via mailing list)
I was trying to submit to program for the prime number challenge, but I
can't make it work because I don't understand how does this website do
the
input to the submitted programs.
I mean that when I use ARGV or $*, it seems these variables are empty
(just
submitting a test program that does a "pp ARGV" outputs an empty array).

Is there anyone who knows how can I get the input for this website ?
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-03-05 17:12
(Received via mailing list)
On 3/5/07, Guillaume Nargeot
<guillaume.nargeotDONOTFUCKINGSPAM@fusionsystems.co.jp> wrote:
> I was trying to submit to program for the prime number challenge, but I
> can't make it work because I don't understand how does this website do the
> input to the submitted programs.
> I mean that when I use ARGV or $*, it seems these variables are empty (just
> submitting a test program that does a "pp ARGV" outputs an empty array).
>
> Is there anyone who knows how can I get the input for this website ?
>
>
Did you try to read from $stdin? I know that is kind of stupid to ask,
but sometimes we do not see what is hidden in front of our eyes :)

Robert
88521907e2c9c585bc94e35a38893dc5?d=identicon&s=25 Jenda Krynicky (jendaperl)
on 2007-03-07 15:24
Brian Adkins wrote:
> This is really bugging me. Someone posted a golf challenge to write a
> smallest FizzBuzz program here:
> ...

Hey! Stop this! It's supposed to be the dreadfull scripting language
called Pearl that's totally unreadable and it's abusers waste the
precious time trying to beat each other by writing something in as few
characters as possible!

:-P

Jenda
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (Guest)
on 2007-03-07 15:32
(Received via mailing list)
On 3/7/07, Jenda Krynicky <jenda@cpan.org> wrote:
>
> Hey! Stop this! It's supposed to be the dreadfull scripting language
> called Pearl that's totally unreadable and it's abusers waste the
> precious time trying to beat each other by writing something in as few
> characters as possible!

The email address simply drips irony :)

m.
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2007-03-07 20:02
(Received via mailing list)
On 3/7/07, Martin DeMello <martindemello@gmail.com> wrote:
> On 3/7/07, Jenda Krynicky <jenda@cpan.org> wrote:
> >
> > Hey! Stop this! It's supposed to be the dreadfull scripting language
> > called Pearl that's totally unreadable and it's abusers waste the
> > precious time trying to beat each other by writing something in as few
> > characters as possible!
>
> The email address simply drips irony :)

And what's pearl?  I thought it was perl.  What does it mean if one
doesn't know how to properly spell one's favorite language?

You could always tell who was really in the Smalltalk community or at
least who wasn't when they spelled it SmallTalk.
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-03-07 20:09
(Received via mailing list)
On 3/7/07, Rick DeNatale <rick.denatale@gmail.com> wrote:
> And what's pearl?  I thought it was perl.  What does it mean if one
> doesn't know how to properly spell one's favorite language?
>
> You could always tell who was really in the Smalltalk community or at
> least who wasn't when they spelled it SmallTalk.
>
> --
> Rick DeNatale
>
>
Oh Rick just forget him, he will become a stone in the sun.

R
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2007-03-07 20:51
(Received via mailing list)
On 3/7/07, Robert Dober <robert.dober@gmail.com> wrote:

> Oh Rick just forget him, he will become a stone in the sun.

Why does conjure up a picture of Mick Jagger lying on a beach?
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-03-07 21:36
(Received via mailing list)
On 3/7/07, Rick DeNatale <rick.denatale@gmail.com> wrote:
> On 3/7/07, Robert Dober <robert.dober@gmail.com> wrote:
>
> > Oh Rick just forget him, he will become a stone in the sun.
>
> Why does conjure up a picture of Mick Jagger lying on a beach?
>
> --
> Rick
>
>
I guess I lack some cultural reference to understand this :)
I was just talking about Trolls as in Tollkien et al..

R.
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2007-03-08 01:09
(Received via mailing list)
On 3/7/07, Robert Dober <robert.dober@gmail.com> wrote:
> On 3/7/07, Rick DeNatale <rick.denatale@gmail.com> wrote:
> > On 3/7/07, Robert Dober <robert.dober@gmail.com> wrote:
> >
> > > Oh Rick just forget him, he will become a stone in the sun.
> >
> > Why does conjure up a picture of Mick Jagger lying on a beach?
> >
> I guess I lack some cultural reference to understand this :)
> I was just talking about Trolls as in Tollkien et al..

Mick Jagger is the lead singer of the Rolling Stones.

Robert my friend you must be either very old or very young (more likely)

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/
Fd22ee3cfc7dac283ce8e451af324f7d?d=identicon&s=25 Chad Perrin (Guest)
on 2007-03-08 07:27
(Received via mailing list)
On Thu, Mar 08, 2007 at 04:01:46AM +0900, Rick DeNatale wrote:
> And what's pearl?  I thought it was perl.  What does it mean if one
> doesn't know how to properly spell one's favorite language?

Actually, in this context, I think "Jenda" is being intentionally
ironic.  That's a jab at people who complain about Perl being unreadable
and suitable only for golf and obfu.
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-03-08 08:49
(Received via mailing list)
On 3/8/07, Rick DeNatale <rick.denatale@gmail.com> wrote:
>
> Mick Jagger is the lead singer of the Rolling Stones.
>
> Robert my friend you must be either very old or very young (more likely)

Actually I am quite old (not as old as the Stones but almost), but
incredibly stupid, I just did not get the pun!!!!
>
> --
> Rick DeNatale
Thx for bringing the light to the blind.

Robert
This topic is locked and can not be replied to.