Skip to content

Instantly share code, notes, and snippets.

@mcelaney
Created August 31, 2016 11:42
Show Gist options
  • Save mcelaney/7fe5821a7f68353015e30af3b1558bb6 to your computer and use it in GitHub Desktop.
Save mcelaney/7fe5821a7f68353015e30af3b1558bb6 to your computer and use it in GitHub Desktop.
Slow - but beautiful in a way
class FizzBuzz
def self.find_answers_for(max)
self.new.find_answers_for(max)
end
def find_answers_for(max)
1.upto(max).map do |current|
[find_result_hash(current), current]
.find { |result| result != '' }
end
end
private
def find_result_hash(number)
{ 3 => "fizz", 5 => "buzz" }
.select { |match| number % match == 0 }
.values
.join('')
end
end
puts Benchmark.measure { FizzBuzz.find_answers_for(1000000) }
# 2.720000 0.030000 2.750000 ( 2.757236)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment