[QUIZ] FizzBuzz (#126) [SOLUTION]

D

doug meyer

This is one of those quizzes that is so easy, you have to find some
ridiculous way of solving it :-D

First off, there is the correct way of coding: clear, concise, and
actually works

#!/usr/bin/env ruby
# Douglas Meyer

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

Then there is the solution I would NEVER give during an interview or
use in a application (unless I had tests, and I was the only one
programming the application). I think this is the first time I used
zip, also the |x,*y| makes me think of erlang, lol

#!/usr/bin/env ruby
# Douglas Meyer

(1..100).zip((([nil]*2<<'Fizz')*34),([nil]*4<<'Buzz')*20){|x,*y| puts
(y.compact.empty? ? x : y.join())}
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top