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

La solución más simple que se me ocurre es:

(1..100).each do |i|
  sound = ''
  sound += 'Fizz' if (i%3).zero?
  sound += 'Buzz' if (i%5).zero?
  if sound.empty?
    puts i
  else
    puts sound
  end
end

(algo piola es que, siguiendo el espíritu del juego, se pueden
contemplar nuevos casos solamente añadiendo 1 línea de código)

BTW, lo que el artículo citado trata de destacar es que increiblemente
muchos programadores tienen problemas para resolver problemas tan
triviales como éste. Por mi parte, hice el experimento con algunos
conocidos (una muestra para nada significativa, lo se) y los
resultados no fueron muy alentadores...

Saludos a todos,

-- 
Javier Smaldone
[EMAIL PROTECTED]
http://www.smaldone.com.ar/ - http://blog.smaldone.com.ar
_______________________________________________
ruby mailing list
[email protected]
http://lista.rubyargentina.com.ar/listinfo.cgi/ruby-rubyargentina.com.ar

Responder a