Skip to content

Instantly share code, notes, and snippets.

@audibleblink
Last active Dec 31, 2015
Embed
What would you like to do?
Big O practice with FizzBuzz
def mult_generator limit, multiple
0.step(limit, multiple).to_a
end
def fizzbuzzify values_hash
values_hash.each_pair do |k,v|
v.each { |v| @answer[v-1] = k.to_s }
end
@answer
end
def bzzt! limit, mult_ary
@answer = (1..limit).to_a
m1 = mult_ary[0] ; m2 = mult_ary[1]
values = {
fizz: mult_generator(limit, m1),
buzz: mult_generator(limit, m2),
fizzbuzz: mult_generator(limit, m1*m2)
}
fizzbuzzify values
end
# Swapped out Array#&
#def bzzt! limit, mult_ary
# @answer = (1..limit).to_a
# values = {
# fizz: mult_generator(limit, mult_ary[0]),
# buzz: mult_generator(limit, mult_ary[1])
# }
# values[:fizzbuzz] = values[:fizz] & values[:buzz]
# fizzbuzzify values
#end
bzzt! 1000, [5,7]
### Benchmark results ###
# "1000, 5, 7"
# 0.000000 0.000000 0.000000 ( 0.000459)
# "5000, 5, 7"
# 0.000000 0.010000 0.010000 ( 0.003200)
# "10000, 5, 7"
# 0.010000 0.000000 0.010000 ( 0.002955)
# "100000, 5, 7"
# 0.030000 0.000000 0.030000 ( 0.032995)
# "10,000,000, 5, 7"
# 5.850000 0.590000 6.440000 ( 6.450806)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment