[QUIZ] FizzBuzz (#126)

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.

In message <20070601122814.XMOR28308.eastrmmtao105.cox.net@eastrmimpo02.cox.net>, Ruby Quiz wri
tes:

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.

For extra fun, try to do it so the core loop is:

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

-s

Ruby Quiz wrote:

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.

Done, 4 times over. Not a bad quiz though my answers will probably be considered boring and would be denounced by the fools using this tactic to hire people as "unimaginative" or "lacking creativity and whizbang problem-solving skills".

I feel compelled to point out that it was Microsoft who started this whole mess, and they quickly figured out it was selecting the wrong people and they don't do it anymore. But the cat's out of the bag, and now every wannabe shop thinks that's the way to find smart people. It's not, it's the way to find people who are good at solving puzzles under pressure, and in the worst case to find people who are good at finding ridiculously clever (but unreadable/unmaintainable) answers to puzzles under pressure.

As we all know, good software development is not about solving little puzzles. It's about reasoning about problems large and small and finding the most obvious and readable solution (and sometimes optimizing the inner loop).

Maybe I would try this in a job interview. I might state the problem and say: "this is not a trick question nor hard - just write the code they way you normally would." If they write the boring answer, they get points in my book. If they write some clever answer, they get demerits. If they can't write code at all, and I didn't realize they wouldn't be able to, I go sit in a corner and pout.

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/

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.

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

Regards, Morton

···

On Jun 1, 2007, at 8:28 AM, Ruby Quiz 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”.

Ruby Quiz <james@grayproductions.net> 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...

···

--
s=%q( Daniel Martin -- martin@snowplow.org
       puts "s=%q(#{s})",s.to_a.last )
       puts "s=%q(#{s})",s.to_a.last

I didn't want to post mine as there was to much golfing around :wink:

But ok...

Here would be my solution (I don't like it specially, but I hope it'll do when the recruiters come :wink: ):

1.upto(100) do |number|
   print "Fizz" if number % 3 == 0
   print "Buzz" if number % 5 == 0
   print number if number % 3 != 0 && number % 5 != 0
   puts ""
end

···

----
Enrique Comba Riepenhausen
ecomba@mac.com

I always thought Smalltalk would beat Java, I just didn't know it would be called 'Ruby' when it did.
-- Kent Beck

Well, It's a few minutes early, but I've got an early morning tomorrow.

Here's three attempts from me.

First is my initial shot, which will cause many of you to cringe at the use of nested ternary operators...
Second, I decided to mess with Fixnum.
Then I got a little bored and thought of something for our friends at thedailywtf (still can't take to the new name)

I'm willing to go with whoever suggested this might bring in the largest number of RubyQuiz responses yet.

Cheers,
Dave

···

#-------------------------------------------------
#Quick first attempt

1.upto(100) {|i| puts(i%15==0 ? 'FizzBuzz' : i%5==0 ? 'Buzz' : i%3==0 ? 'Fizz' : i)}

#-------------------------------------------------
#Lets play with Fixnum

class Fixnum
   def fizz_buzzed
     a= (self%3==0 ? 'Fizz' : "")
     a+= 'Buzz' if self%5==0
     a= self.to_s if a==""
     a
   end
end

1.upto(100) {|i| puts i.fizz_buzzed}

#-------------------------------------------------
# How about something for thedailywtf.com?

def divisible_by_3(i)
   if i.to_s.size>1 then
     divisible_by_3(i.to_s.split(//).map{|char| char.to_i}.inject{|a,b| a+b})
   else
     [3,6,9].include?(i)
   end
end

def divisible_by_5(i)
   ['0','5'].include?(i.to_s[-1].chr)
end

def divisible_by_15(i)
   divisible_by_3(i) && divisible_by_5(i)
end

1..100.each do |i|
    if divisible_by_15(i) then puts 'FizzBuzz'
    elsif divisible_by_3(i) then puts 'Fizz'
    elsif divisible_by_5(i) then puts 'Buzz'
    else puts i
    end
end

Hi everyone,

Here's my solution. It's the first one that occurred to me, and it only took a few mins. to write.

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

I tried it a few other ways just for fun. I'm happy with this one that uses 'case.'

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

Regards,
Craig

Here are my solutions to Quiz 126. Because it was the one that popped into my mind when I read the quiz description, the first is the one that I think I'd produce if challenged during a job interview.

<code>
# Simple, obvious (to me) way to do it.
(1..100).each do |n|
    case
    when n % 15 == 0 : puts 'FizzBuzz'
    when n % 5 == 0 : puts 'Buzz'
    when n % 3 == 0 : puts 'Fizz'
    else puts n
    end
end

# Not quite so obvious, but a little DRYer:
(1..100).each do |n|
    puts case
          when n % 15 == 0 : 'FizzBuzz'
          when n % 5 == 0 : 'Buzz'
          when n % 3 == 0 : 'Fizz'
          else n
          end
end

# Of course, could do it with if-elsif-else ...
(1..100).each do |n|
    puts(
       if n % 15 == 0
          'FizzBuzz'
       elsif n % 5 == 0
          'Buzz'
       elsif n % 3 == 0
          'Fizz'
       else n
       end
    )
end

# ... or even with the ternary operator.
(1..100).each do |n|
    puts(
       n % 15 == 0 ? 'FizzBuzz' :
       n % 5 == 0 ? 'Buzz' :
       n % 3 == 0 ? 'Fizz' : n
    )
end
</code>

Regards, Morton

that's my clear and straight-forward solution, I would give on a job interview:

#!/usr/bin/env ruby

$VERBOSE = true

1.upto(100) do |num|

   case when num.modulo(3) == 0 && num.modulo(5) == 0 :puts 'fizzbuzz'

         when num.modulo(3) == 0 :puts 'fizz'

         when num.modulo(5) == 0 :puts 'buzz'

         else puts num
   end

end

···

--
greets

                     one must still have chaos in oneself to be able to give birth to a dancing star

Ruby Quiz|

Everything is supposed to be a "FizzBuzz" unless having been declared
something other.

···

----
def fizzbuzz(value)
  result = "FizzBuzz"
  result.gsub!("Buzz", "") if value % 5 != 0
  result.gsub!("Fizz", "") if value % 3 != 0
  result = value if result.empty?
  result
end

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

--
I. P. 2007-06-03T17:06

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

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 == 0 then
    print "Fizz"
  end
  if n % 5 == 0 then
    print "Buzz"
  end
  if (n%3 != 0) and (n%5 != 0) then
    print n
  end
  print "\n"
}

# Second Attempt

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

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.

···

--
Bill Guindon (aka aGorilla)
The best answer to most questions is "it depends".

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.

···

On 6/1/07, Ruby Quiz <james@grayproductions.net> wrote:

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.

--
Chris Carter
concentrationstudios.com
brynmawrcs.com

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.

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

···

On Fri, 01 Jun 2007 21:28:14 +0900, Ruby Quiz wrote:
=-=-=-=-=

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

--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/

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"