[QUIZ] FizzBuzz (#126)

M

Michael Glaesemann

My first Ruby Quiz submission.

# Straightforward solution

(1..100).each do |n|
print "Fizz" if 0 == n % 3
print "Buzz" if 0 == n % 5
print n if 0 != n % 3 and 0 != n % 5
print "\n"
end

# Peter Seebach "extra fun" solution

class Fixnum
alias old_to_s to_s

def to_s
value = ""
value += "Fizz" if 0 == self % 3
value += "Buzz" if 0 == self % 5
value += self.old_to_s if "" == value
value
end
end

(0..100).each { |x| p x }

# make things right again
class Fixnum
alias to_fizz_buzz to_s
alias to_s old_to_s
end

# golf solution (67 chars)
1.upto(?d){|n|puts 0<n%3&&0<n%5?n:(1>n%3?"Fizz":'')+(1>n%5?"Buzz":'')}

Cheers,

Michael Glaesemann
grzm seespotcode net
 
J

Joshua Ballanco

So, I've got to lay claim to the 56 byte solution:

sh-3.2$ wc fizzbuzz.rb
0 2 56 fizzbuzz.rb
sh-3.2$ ruby fizzbuzz.rb | tail -11
"FizzBuzz"
91
92
"Fizz"
94
"Buzz"
"Fizz"
97
98
"Fizz"
"Buzz"

...but I'm not sure if I should post it. Would people consider that a
spoiler?
 
R

Robert Dober

So, I've got to lay claim to the 56 byte solution:

sh-3.2$ wc fizzbuzz.rb
0 2 56 fizzbuzz.rb
sh-3.2$ ruby fizzbuzz.rb | tail -11
"FizzBuzz"
91
92
"Fizz"
94
"Buzz"
"Fizz"
97
98
"Fizz"
"Buzz"
Have been there, but this is not a valid solution, sorry to say so :(
you gotta get rid of the quotes in the output.
...but I'm not sure if I should post it. Would people consider that a
spoiler?
The non spoiler period is over already ;)

Cheers
Robert
 
W

William (Bill) Froelich

Here was my first attempt that was around 5 minutes of effort. After
thinking about it more I ended up writing a second version (also below)
that was less creative.

--Bill

# First attempt

(1..100).each { |n|
if n % 3 =3D=3D 0 then
print "Fizz"
end
if n % 5 =3D=3D 0 then
print "Buzz"
end
if (n%3 !=3D 0) and (n%5 !=3D 0) then
print n
end
print "\n"
}


# Second Attempt

(1..100).each { |n|
case
when (n%3 =3D=3D 0) && (n%5 =3D=3D 0) then
puts "FizzBuzz"
when (n%3 =3D=3D 0) then
puts "Fizz"
when (n%5 =3D=3D 0) then
puts "Buzz"
else
puts n
end
}
 
B

Bill Guindon

It's been months, and everbody else is, so why not...

(1..100).each do |x|
m3 = x.modulo(3) == 0
m5 = x.modulo(5) == 0

puts case
when (m3 and m5) then 'FizzBuzz'
when m3 then 'Fizz'
when m5 then 'Buzz'
else x
end
end

I went for clarity and simplicity.
 
M

Michael Glaesemann

if only 'puts'.length were 'p'.length ;)

s/proper solution/expected results/

And 59 isn't bad, I should think :p

Michael Glaesemann
grzm seespotcode net
 
J

James Edward Gray II

A non-golf solution of mine :

Just as a reminder, please check that the no spoiler period has
expired before posting quiz solutions. Thank you.

James Edward Gray II
 
P

Patrick Hurley

My first thought:

(1..100).each do |i|
case
when i % 15 == 0
puts "FizzBuzz"
when i % 3 == 0
puts "Fizz"
when i % 5 == 0
puts "Buzz"
else
puts i
end
end

If I took 30 seconds to think about it on an interview, I would have a
unit test (see my earlier post) and something like this:

module Enumerable
def map_every(n)
m = n - 1
result = []

self.each_with_index do |elem,i|
if i % n == m
result << yield(elem,i)
else
result << elem
end
end

result
end
end

result = *1..100
result = result.map_every(3) { "Fizz" }
result = result.map_every(5) { "Buzz" }
result = result.map_every(15) { "FizzBuzz" }
puts result
 
K

Ken Bloom

The three rules of Ruby Quiz:

1. Please do not post any solutions or spoiler discussion for this quiz
until 48 hours have passed from the time on this message.

2. Support Ruby Quiz by submitting ideas as often as you can:

http://www.rubyquiz.com/

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem helps
everyone on Ruby Talk follow the discussion. Please reply to the
original quiz message, if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- =-=-=-=-=

There has been some debate on the proper ways to screen programmers you
intend to hire. A common theory is that you really need to have the
programmer write some code for you to accurately gauge their skill.
Exactly what to have them write is another debate, but the blogosphere
has recently been abuzz with this question as a screener:

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â€.

Pretend you've just walked into a job interview and been hit with this
question. Solve it as you would under such circumstances for this week's
Ruby Quiz.

class Integer
def === num
num % self == 0
end
end

100.times do |x|
case x
when 15: puts "FizzBuzz"
when 3: puts "Fizz"
when 5: puts "Buzz"
else puts x
end
end
 
C

Chris Carter

The three rules of Ruby Quiz:

1. Please do not post any solutions or spoiler discussion for this quiz until
48 hours have passed from the time on this message.

2. Support Ruby Quiz by submitting ideas as often as you can:

http://www.rubyquiz.com/

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone
on Ruby Talk follow the discussion. Please reply to the original quiz message,
if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

There has been some debate on the proper ways to screen programmers you intend
to hire. A common theory is that you really need to have the programmer write
some code for you to accurately gauge their skill. Exactly what to have them
write is another debate, but the blogosphere has recently been abuzz with this
question as a screener:

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

Pretend you've just walked into a job interview and been hit with this question.
Solve it as you would under such circumstances for this week's Ruby Quiz.

Here is my nice, ungolfed version. But I took Peter's Extra Credit challenge.

class Integer
def inspect
x = (self % 3 == 0 ? "Fizz" : "")
x << ( self % 5 == 0 ? "Buzz" : "" )
x.empty? ? self : x
end
end

(1..100).each {|x| p x}

I did this live in front of a friend, then he tried it in java. And
Obj-C. I beat him in time and line count for both. It was pretty
funny to watch him try.
 
M

Michael Glaesemann

# golf solution (67 chars)
1.upto(?d){|n|puts 0<n%3&&0<n%5?n:(1>n%3?"Fizz":'')+(1>n%5?"Buzz":'')}

I can't count today: 70 chars.

Michael Glaesemann
grzm seespotcode net
 
J

Joshua Ballanco

Michael said:
s/proper solution/expected results/

And 59 isn't bad, I should think :p

Michael Glaesemann
grzm seespotcode net

Thanks. I'd push it a bit further, but I think I've already wasted
enough time golfing. This has gotten me thinking though...

Mathematically speaking, finding the shortest/optimal solution to any
programming challenge is an NP problem. It might be fun to do a follow
up quiz where the goal is to write a code generator that can find the
elusive 56 byte golfing solution to the FizzBuzz quiz. If one were to
simply write a random ASCII string generator and test every possible 56
byte string, it would take less than 95^56 (or 5.656e110) iterations...

...or would that quiz be too hard?

;)

-Josh
 
H

Hans Fugal

Daniel said:
The point is, yes, maintainability is important, but I really don't
understand the animosity towards "cleverness" that seems to be showing
up here. That sounds to me dangerously close to the same attitude
that keeps ruby and many other non-mainstream languages out of many
production environments on the grounds that "we'll never be able to
find anyone to maintain it".

I suppose I should clarify what I mean by cleverness. I distinguish
between "clever" code and elegant code. Code that might be considered
clever but is in good taste I would term elegant. Golfing, fancy bit
manipulation by C programmers where the compiler would take care of it
anyway, etc. I term clever with a derogatory tone. The choice of terms
is almost entirely arbitrary. The point is that, as you said, in a job
interview what you want is a straightforward test of ability, not to
assess for on-the-spot puzzle solving ability.
Also, as a practical matter, I haven't seen this "cleverness" in any
of the subsequent interviews I've conducted from the other side of the
desk. Frankly, I'd love some evidence that incoming candidates had
been exposed to something other than the industry standard languages
and platforms. Exposure to functional programming and the thought
patterns that go with it is a *good* thing. Being able to look at a
problem from multiple angles is a *good* thing. Mental agility?
We're supposed to like that in a candidate.

Ya, that'd be elegant. :)
 
H

Hans Fugal

My solutions.

1.upto(100) do |i|
if i%3 == 0
if i%5 == 0
puts "FizzBuzz"
else
puts "Fizz"
end
elsif i%5 == 0
puts "Buzz"
else
puts i
end
end

# ok, great, now pretend you're a moron and want to write it in a clever way to
# optimize efficiency because the mod operation is slow on your imaginary
# pathological machine

a = 1
b = 1
1.upto(100) do |i|
if a == 3
if b == 5
puts "FizzBuzz"
else
puts "Fizz"
end
elsif b == 5
puts "Buzz"
else
puts i
end

a += 1
a = 1 if a > 3

b += 1
b = 1 if b > 5
end

# ok, great, but now what happens if you have to do it recursively because, um,
# because... because *I* am a moron. (me: I could have told you that back when
# we started this process)

def fizzbuzz(i)
fizzbuzz(i-1) unless i <= 1
if i%3 == 0
if i%5 == 0
puts "FizzBuzz"
else
puts "Fizz"
end
elsif i%5 == 0
puts "Buzz"
else
puts i
end
end
fizzbuzz(100)

# ok, great, now can you do that without the mods? You're doing fine, we just
# want to see how well you bend to the will of your boss when asked to do
# menial and pointless code changes

puts "I've seen enough. I think you'd better find another monkey because I'm not\ninterested in working for you."

# Walking out the door...
# Stranger: "Psst! Hey, try doing it so the core loop is:
# (1..100).each {|x| p x}"
# Me: "I should deck you and walk away, but that's almost interesting..."
class Numeric
alias :eek:ld_inspect :inspect
def inspect
if self % 3 == 0
if self % 5 == 0
"FizzBuzz"
else
"Fizz"
end
elsif self % 5 == 0
"Buzz"
else
old_inspect
end
end
end

(1..100).each {|x| p x}
 
Y

Yossef Mendelssohn

I have two solutions. One is the quick job interview version:

(1..100).each { |x| str = ''; str += 'Fizz' if (x % 3).zero?; str +=
'Buzz' if (x % 5).zero?; str = x if str.empty?; puts str }


The second is inspired by Peter Seebach's early suggestion for "extra
fun". It's not much different from the above, but it puts the logic
in Fixnum:

class Fixnum
def to_s
str = ''
str += 'Fizz' if (self % 3).zero?
str += 'Buzz' if (self % 5).zero?
str = '%d' % self if str.empty?
str
end
end

(1..100).each { |x| puts x }


My favorite thing about that solution is seeing irb prompts like
"irb(main):013:FizzBuzz>" and seeing that the range is output as
"1..Buzz"
 
B

Brian Candler

My solution: 63 characters without the quotes, or 60 characters with them.

1.upto(100){|i|i%3==0&&x="Fizz";i%5==0&&x="#{x}Buzz";puts x||i}

1.upto(100){|i|i%3==0&&x="Fizz";i%5==0&&x="#{x}Buzz";p x||i}
 

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

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,592
Members
45,103
Latest member
VinaykumarnNevatia
Top