FizzBuzz (#126)

On Jun 3, 2007, at 9:40 , Joshua B. wrote:

Robert D. wrote:

Have been there, but this is not a valid solution, sorry to say so :frowning:
you gotta get rid of the quotes in the output.
Crud, I just tested at anarchy golf - FizzBuzz and you’re
right…

For those interested, the code was:

1.upto(?d){|i,x|i%5>0||x=:Buzz;p i%3>0?x||i:“Fizz#{x}”}

s/p/puts/ and you get the proper solution:

1.upto(?d){|i,x|i%5>0||x=:Buzz;puts i%3>0?x||i:“Fizz#{x}”}

Michael G.
grzm seespotcode net

On 6/3/07, Michael G. [email protected] wrote:

1.upto(?d){|i,x|i%5>0||x=:Buzz;p i%3>0?x||i:“Fizz#{x}”}

s/p/puts/ and you get the proper solution:
if only ‘puts’.length were ‘p’.length :wink:

On Jun 3, 2007, at 9:56 , Robert D. wrote:

right…

For those interested, the code was:

1.upto(?d){|i,x|i%5>0||x=:Buzz;p i%3>0?x||i:“Fizz#{x}”}

s/p/puts/ and you get the proper solution:
if only ‘puts’.length were ‘p’.length :wink:

s/proper solution/expected results/

And 59 isn’t bad, I should think :stuck_out_tongue:

Michael G.
grzm seespotcode net

Here is my 56B

1.upto(?d){|x|x%5>0||b=:Buzz;puts x%3>0?b||x:“Fizz%s”%b}

I think my code looks similar to Joshua’s…

2007/6/3, Sun P. [email protected]:

On Jun 3, 2007, at 6:07 AM, Sun P. wrote:

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 G. II

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


I peeked on the mailing list, so I won’t participate in the golf
tournament – and I am not particularly good at golfing in any case.

pth

On Jun 3, 2007, at 8:17 , Michael G. wrote:

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 G.
grzm seespotcode net

On 6/1/07, Ruby Q. [email protected] wrote:

question as a screener:

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.

Michael G. wrote:

s/proper solution/expected results/

And 59 isn’t bad, I should think :stuck_out_tongue:

Michael G.
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?

:wink:

-Josh

On Fri, 01 Jun 2007 21:28:14 +0900, Ruby Q. wrote:

Suggestion: A [QUIZ] in the subject of emails about the problem helps
everyone on Ruby T. 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 Q…

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

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 S.'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”

Daniel M. wrote:

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. :slight_smile:

My first, obvious solution:

(1…100).each do |i|
if (i % 3 == 0) and (i % 5 == 0)
puts “FizzBuzz”
elsif (i % 3 == 0)
puts “Fizz”
elsif (i % 5 == 0)
puts “Buzz”
else
puts i
end
end

Trying to reduce the redundant ifs:

(1…100).each do |i|
s = ‘’
s << “Fizz” if (i % 3 == 0)
s << “Buzz” if (i % 5 == 0)
puts(s == ‘’ ? i : s)
end

The above seems unique among the solutions I’ve read so far.

Ryan

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}

On Sun, Jun 03, 2007 at 11:40:51PM +0900, Joshua B. wrote:

Crud, I just tested at anarchy golf - FizzBuzz and you’re
right…

For those interested, the code was:

1.upto(?d){|i,x|i%5>0||x=:Buzz;p i%3>0?x||i:“Fizz#{x}”}
^^
Why the ,x here? x is locally scoped within the block anyway, and it
works
without it.

This solution cracked me up, but there is a typo in it…

On Jun 3, 2007, at 9:49 AM, Fred P. wrote:

puts ‘59’
puts 'FizzBuzz
puts ‘61’

The middle line there is missing a quote. :wink:

James Edward G. II

my first solution:
(1…100).each do |n|
print :Fizz if (n % 3) == 0
print :Buzz if (n % 5) == 0
print n if (n % 3) != 0 and (n % 5) != 0
print “\n”
end

and my second one:
(1…100).each { |n| puts n % 3 == 0 ? n % 5 == 0 ? :FizzBuzz : :Fizz :
n % 5 == 0 ? :Buzz : n }

greets, paddor

2007/6/1, Ruby Q. [email protected]:

On 6/1/07, Ruby Q. [email protected] wrote:

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 Q…

I’d probably want to do something a little clever but not anything
too unorthodox, so here’s my solution:

def replace(n,list=[[“FizzBuzz”,15],[“Buzz”,5],[“Fizz”,3]])
list.each { |r,m| return r if n % m == 0 }
return n
end

puts (1…100).map { |e| replace(e) }

On 6/3/07, James Edward G. II [email protected] wrote:

This solution cracked me up, but there is a typo in it…

On Jun 3, 2007, at 9:49 AM, Fred P. wrote:

puts ‘59’
puts 'FizzBuzz
puts ‘61’

The middle line there is missing a quote. :wink:

But would you hire him (minus the typo)? I’d be a bit worried that
he’d shown up for a job interview with a hangover :wink:

Awesome!