Skip to content

Instantly share code, notes, and snippets.

@mcelaney
Created August 31, 2016 11:34
Show Gist options
  • Save mcelaney/d20353996877d5603107c6f5ee889c35 to your computer and use it in GitHub Desktop.
Save mcelaney/d20353996877d5603107c6f5ee889c35 to your computer and use it in GitHub Desktop.
Ugly but fast
class FizzBuzz
def self.find_answers_for(max)
self.new.find_answers_for(max)
end
def initialize
@result = [
Proc.new do |x| x; end,
Proc.new do |x| x; end,
Proc.new do |x| "Fizz"; end,
Proc.new do |x| x; end,
Proc.new do |x| "Buzz"; end,
Proc.new do |x| "Fizz"; end,
Proc.new do |x| x; end,
Proc.new do |x| x; end,
Proc.new do |x| "Fizz"; end,
Proc.new do |x| "Buzz"; end,
Proc.new do |x| x; end,
Proc.new do |x| "Fizz"; end,
Proc.new do |x| x; end,
Proc.new do |x| x; end,
Proc.new do |x| "FizzBuzz"; end,
]
end
def find_answers_for(max)
1.upto(max).map { |x| @result[(x - 1) % 15].call(x) }
end
end
puts Benchmark.measure { FizzBuzz.find_answers_for(1000000) }
# 0.200000 0.010000 0.210000 ( 0.213481)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment