Given the following variables divisor_array, low, high define a method that
- Prints all numbers from low to high
- If the any number being printed is divisible by any divisor number in
divisor_array
, print the number + the word "fizzy" - If the number being printed is divisible by ALL the numbers in the array, it should output the number + "reallyfizzy".
Testing Values: generally_fizzy([2,3],1,7)
My initial solution:
def generally_fizzy(divisor_array, low, high)
divisors = Hash[*divisor_array]
low.upto(high) do |i|
divisors.each_pair do |k,v|
if((i % k == 0) && (i % v == 0))
puts "#{i} reallyfizzy"
elsif ((i % k == 0) || (i % v == 0))
puts "#{i} fizzy"
else
puts i
end
end
end
end
this solution passes the tests given, but when the divisor_array
size is increased from 2 values to 3 and over it prints out duplicates. In addition to that, the code is not very elegant.
Looking for a working alternative, that can deal with divisor_array
size changes.