FizzBuzz (#126)

Like many others, I knocked this out in a few minutes, while I was
eating lunch.

I then decided, like others, to try to do it different ways. I came
up with 6 different solutions in about an hour, ranging from quite
straightforward to bizarre and cryptic.

I hope that those who are familiar with my musings, ponderings and
pontifications will realize that the latter ones were out of my
wheelhouse.

ISTR that this problem came up not too long ago in the code golfing
community. I won’t go that far!


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

On 6/1/07, Daniel M. [email protected] wrote:

Incidentally, my initial answer was:

(Plus @@(Min /@ #))&

Anyone care to identify the language?

Mathematica.

On 6/1/07, Daniel M. [email protected] wrote:

Incidentally, my initial answer was:

(Plus @@(Min /@ #))&
APL? A wild guess though.

Anyone care to identify the language?


s=%q( Daniel M. – [email protected]
puts “s=%q(#{s})”,s.to_a.last )
puts “s=%q(#{s})”,s.to_a.last

Robert

Hey all, I have a quick question on this quiz:
I have a working version of it, but I just want to compare and see
what everyone else got.

How many lines do you have it written in?

That’s my big problem - making programs too long.

thanks,
~ Ari
English is like a pseudo-random number generator - there are a
bajillion rules to it, but nobody cares.

On 6/1/07, Robert D. [email protected] wrote:


– George Bernard Shaw

APL was my guest too.

quoth the Ari B.:

English is like a pseudo-random number generator - there are a
bajillion rules to it, but nobody cares.

Mine is 8 lines (not including the shebang) two of which are 'end’s. I
wrote
it in probably the most obvious way, so it’s certainly not ‘clever’ or
anything.

-d

From: “Ari B.” [email protected]

Hey all, I have a quick question on this quiz:
I have a working version of it, but I just want to compare and see
what everyone else got.

How many lines do you have it written in?

I typed it in as a one-liner in IRB just out of curiosity. I figured
it was some kind of trick question, so I tried to be as non-clever as
possible.

After that I made a slightly shorter version of the one-liner just to
eliminate some duplication. But still nothing very clever. :slight_smile:

Regards,

Bill

On 6/1/07, darren kirby [email protected] wrote:

~ Ari
English is like a pseudo-random number generator - there are a
bajillion rules to it, but nobody cares.

Mine is 8 lines (not including the shebang) two of which are 'end’s. I wrote
it in probably the most obvious way, so it’s certainly not ‘clever’ or
anything.

As I said, I wrote 6 different versions, all of them are methods on
Integer, and a (1…100).each {|i| p i.fizz_buzzn)

where n is an integer.

The most obvious method has 8 lines inside the def/end, while the
shortest has one.

Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

“Sander L.” [email protected] writes:

On 6/1/07, Daniel M. [email protected] wrote:

Incidentally, my initial answer was:

(Plus @@(Min /@ #))&

Anyone care to identify the language?

Mathematica.

Bingo.

And APL is significantly less legible than this. (Among other things,
I used ASCII there) Even J (APL’s modern, ASCII-based descendant)
would be less legible than that.

On Jun 1, 2007, at 8:28 AM, Ruby Q. wrote:

Write a program that prints the numbers from 1 to 100.
But for multiples of three print “Fizz” instead of the
number and for the multiples of five print “Buzz”. For
numbers which are multiples of both three and five
print “FizzBuzz”.

I would not be surprised if this quiz produces the most submissions
of any quiz ever.

Regards, Morton

Ruby Q. [email protected] writes:

Write a program that prints the numbers from 1 to 100.
But for multiples of three print “Fizz” instead of the
number and for the multiples of five print “Buzz”. For
numbers which are multiples of both three and five
print “FizzBuzz”.

Anyone care for a friendly game of golf?
I’ve got this in 67 characters…

On 6/2/07, Daniel M. [email protected] wrote:

Mathematica.

Bingo.
Do you mean it is Mathematica or do you mean it is Bingo :wink:

Oh well just realized I am a bad loser :wink:
Robert

On Sat, Jun 02, 2007 at 09:42:13PM +0900, [email protected] wrote:

print “FizzBuzz”.

Anyone care for a friendly game of golf?
I’ve got this in 67 characters…

I’m afraid I’m stuck on 71. Is it OK to post solutions here at this
stage?

67 here too, and nothing really clever in it.

$ wc fizzbuzz.rb
0 4 67 fizzbuzz.rb
$ ruby fizzbuzz.rb | tail -5
Fizz
97
98
Fizz
Buzz

Hi –

On Sat, 2 Jun 2007, Daniel M. wrote:

Ruby Q. [email protected] writes:

Write a program that prints the numbers from 1 to 100.
But for multiples of three print “Fizz” instead of the
number and for the multiples of five print “Buzz”. For
numbers which are multiples of both three and five
print “FizzBuzz”.

Anyone care for a friendly game of golf?
I’ve got this in 67 characters…

I’m afraid I’m stuck on 71. Is it OK to post solutions here at this
stage?

David

On Sat, Jun 02, 2007 at 09:06:50PM +0900, Daniel M. wrote:

Ruby Q. [email protected] writes:

Write a program that prints the numbers from 1 to 100.
But for multiples of three print “Fizz” instead of the
number and for the multiples of five print “Buzz”. For
numbers which are multiples of both three and five
print “FizzBuzz”.

Anyone care for a friendly game of golf?
I’ve got this in 67 characters…

Actually, I’ve now got 65 character or 62 characters. The 62 character
version interprets the specification literally by printing “Fizz”,
“Buzz” or
“FizzBuzz” with the double quotation marks :slight_smile:

Hey all,
my first ruby submission is coming up, and i’m pretty psyched - I’m
even cron-ing my email submission to try to be the first one in :smiley:

But I have a problem. In my code, I want to use case - when
statements in it to reduce line wastage. BUT I get an error when I
try to use this sample code:

case s
when s + 1 == y and s - 1 == q

anyhelp?

also, modulus is x % y, right?

thanks,
---------------------------------------------------------------|
~Ari
“I don’t suffer from insanity. I enjoy every minute of it” --1337est
man alive

try &&

[email protected] writes:

I’m afraid I’m stuck on 71. Is it OK to post solutions here at this
stage?

According to the rules posted, we need to wait 48 hours from the
initial posting, which will in my timezone (US Eastern) be at 8:28
tomorrow morning.

At this point, the only things fair to post (based on past quizzes)
would be:

  • sample output
  • unit tests
  • speed measurements

You’d have to be careful, though, to construct a unit test that does
not itself contain a quiz solution. I guess it’s kosher to post this
test program, written in sh:

#! /bin/sh
RUBYSCRIPT=$1
if [ ruby "$RUBYSCRIPT" | wc -l -ne 100 ]; then
echo “Fail linecount”; exit 1
fi
ruby “$RUBYSCRIPT” |
egrep -v ‘^([0-9]+|Fizz|Buzz|FizzBuzz)$’ > /dev/null
if [ $? -eq 0 ]; then
echo “Fail content”; exit 1
fi

now a few spotchecks

spotcheck() {
ruby “$RUBYSCRIPT” | nl | grep “^ $1.$2$" > /dev/null
if [ $? -ne 0 ]; then
echo "Fail $
”; exit 1
fi
}
spotcheck 1 1
spotcheck 3 Fizz
spotcheck 5 Buzz
spotcheck 15 FizzBuzz
spotcheck 23 23
spotcheck 90 FizzBuzz
spotcheck 97 97
spotcheck 98 98
spotcheck 99 Fizz
spotcheck 100 Buzz

echo Passed
exit 0

My solution has 75 characters, damn, I will have another look at it!

to answer ari’s questions:

case s
when s + 1 == y and s - 1 == q

try this:

case
when s+1==y && s-1==q

also, modulus is x % y, right?

yes

James Edward G. II [email protected] writes:

On Jun 2, 2007, at 7:06 AM, Daniel M. wrote:

Anyone care for a friendly game of golf?
I’ve got this in 67 characters…

Is that how you would solve it in your job interview? Just curious.

Well, no. In a job interview I’d give the first solution I came up
with, which is quite boring. At least, initially. If explicitly
asked for different solutions, maybe. At a job interview I’ve alluded
to before, I was asked to write a perl one-liner that did a particular
task, and later as we had some extra time at the end of the interview,
(it was a marathon interview day, with interviews scheduled on the
hour) I spent a minute or two with the interviewer looking at ways to
play perlgolf with my solution.

Basically, trying to golf the solution is a form of joking around with
the interviewer,(*) and like anything of that nature, you do it only
when appropriate.

(*) albeit a highly geeky form of joke