Created
August 31, 2016 11:42
-
-
Save mcelaney/7fe5821a7f68353015e30af3b1558bb6 to your computer and use it in GitHub Desktop.
Slow - but beautiful in a way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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