Plasticx Blog

Capable of being shaped or formed

FizzBuzz Pro, Hire Me

Posted by Mike 06/01/2007 at 09:56AM

I wrote this code in about one minute in response to Ruby QUIZ #126 :

(1..100).each do |i|
  case
  when i % 3 == 0 && i % 5 == 0
    p "FizzBuzz #{i}"
  when i % 3 == 0
    p "Fizz #{i}"
  when i % 5 == 0
    p "Buzz #{i}"
  else
    p i
  end
end

When I wrote it I couldn’t remember if the dot dot .. operator on a range was inclusive or exclusive, fortunately I guessed right.

So you can hire me

The quiz was inspired by this blog post
Why Can’t Programmers.. Program?

My experience has been if you can write white board code fairly well during an interview then you can code fairly well too.

Update
Morton Goldberg wrote many versions but this one with the value to puts chained together with the ternary operator (I think) is The Hot

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

Posted in |

Trackbacks<

Use the following link to trackback from your own site:
http://plasti.cx/trackbacks?article_id=268


Web Statistics