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.
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!
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”.
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
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
#------------------------------------------------- #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
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>
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
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
}
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:
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
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/