News for hackers. Stuff that matters.
Re:Can you beat 99% of programmers? Try the FizzBuzz challenge by cheald on Wednesday December 14, 2011 @07:13PM (#3354386)
The fun thing about these sorts of tests if in figuring out how to do it a little bit differently. For example, I wanted to see if I could do it with a true one-liner (ie, no semicolons) with piecewise construction (ie, no nested ternaries). Here's my solution.

(Yes, it's ugly. But it works!)

    (1..100).each { |i| puts [i % 3 == 0 ? "Fizz" : nil, i % 5 == 0 ? "Buzz" : nil].join.first || i }
Re:Can you beat 99% of programmers? Try the FizzBuzz challenge by Kafka on Monday December 19, 2011 @06:27PM (#3371559)
1.upto(100){|n|puts [["Fizz"][n%3],["Buzz"][n%5]].inject{|m,e|m=m.nil? ? e:m+=e||""}||n}
Re:Can you beat 99% of programmers? Try the FizzBuzz challenge by Kafka on Monday December 19, 2011 @05:20PM (#3371370)
NoMethodError: undefined method `first' for "":String in Ruby 1.9.3