R
Robert Dober
and inject (unless I midded itAnd yet, no one has so far posted a solution involving callcc or
threads. (unless I missed it)
and inject (unless I midded itAnd yet, no one has so far posted a solution involving callcc or
threads. (unless I missed it)
That takes care of inject, what a great response time Donald !I'll add my solutions to the thundering horde. My first solution was
moderately clever:
puts
(1..100).map{|n|[[3,'fizz'],[5,'buzz']].inject(n){|s,a|s.is_a?(Fixnum)?s
=a[1]:s+=a[1] if n%a[0]==0;s}}
then I tried to use as few characters as possible:
puts (1..100).map{|n|n%15==0?:fizzbuzz:n%5==0?:buzz:n%3==0?:fizz:n}
puts (1..100).map{|n|n%15>0?n%5>0?n%3>0?n:'fizz':'buzz':'fizzbuzz'}
- donald
The first question that jumps into my mind is: why would a job
interviewer wanted me to write me such a program? What the heck is a
'FizzBuzz'?
Robert Dober said:and inject (unless I midded it
Aside from the one posted just after you sent this, there actually
have been one or two solutions using inject already, but nothing that
really seems to take advantage of it. Of course, when I try to
construct such a solution, I can't do much better - inject is a
relatively specialized tool, and works best when the output of inject
is then useful in some material way, rather than when it's being
executed purely for its side effects.
Didn't you hear? Google is developing a soft drink containing nothing
but carbonation and caffeine.
containing nothing but carbonation and caffeine.
Honestly the idea is awesome, but he should have taken care to get the
syntax right. I think it is not a good solution in the exact context
but a good distribution for us to have fun.
Einstein who said "As simple(*) as possible, but not simpler!"
the last part being quite irrelevant in our case.
BUT does the merciless application of KISS give you the warm fuzzy
feeling?
If I were on the hiring side in the scenario put forth in the quiz
description, I would look more favorably on a job candidate who came
up with Phillips' solution than one who came up with a more
pedestrian one such my own.
Einstein who said "As simple(*) as possible, but not simpler!"
the last part being quite irrelevant in our case.
BUT does the merciless application of KISS give you the warm fuzzy
feeling?
No, but it makes me laugh when I see it. I admire Phillips' solution
not because I'm a big fan of KISS[*] but for its zen-like qualities
-- the out-of-the-box thinking it displays.
I disagree. I challenged several of my coworkers to solve FizzBuzz a
few weeks ago. All of the solutions were pretty much the same except
the one from our lead DBA. He solved the problem in a stored
procedure without using a cursor. It was exceptionally clever and
totally horrific at the same time. We gave him the "Most
Inappropriate Use of Set Theory" award.
As the last few days have shown, there *are* clever ways of solving
FizzBuzz. However, IMHO, there are very few that would be appropriate
for the stated scenario of a job interview.
My tendency would be to create the simplest thing that could possibly
work in clear and readable and maintainable code. After all,
employers want someone who is smart, but also want someone who gets
things done efficiently. Possibly, with just the right interviewer, I
might discus various solutions pros and cons, which might be optimized
for particular scenarios, etc.
Maybe I just haven't interviewed the right sort of employers that
would appreciate such creativity...
Regards,
Paul.
Actually I feel that somehow - sorry I have said this before, but I am
not sure it was heard - this is about a job, yes but with R u b y.
And if I were to hire a Ruby programmer and the guy just writes some
Java/C/Perl/OCaml or whatever code, expressed in Ruby, will I take
him?
So whatever is said about simple and maintainable and psychological
impact for sure is clever stuff coming from experience I am lacking.
But many of you are forgetting the R u b y part nonetheless.
Cheers
Robert
If I were giving this test, extra credit would be earned for
knowing that i%15 == 0 is equivalent to (i%3==0 && i%5==0) because
I can't imagine a programmer being very good without a firm grasp
of some math as basic as this.
I guess you wouldn't hire me. I used the double-test form because
I thought it was self documenting.
James Edward Gray II
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?
Is there a short circuit AND in ruby?
James said:&& does short-circuit.
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.