I'm attempting to write a method that takes a value N and returns three integers N/2, N/3, and N/4 rounded down. It keeps taking them back until there is only zero.
def crazy_coins(n)
coins = Array.new
coins.push(n.to_a)
generate = Proc.new { |x|
temp = []
count = 2
while count < 5
x = n/count
temp << x
count += 1
end
return temp
}
coins.map!(&generate)
end
Outputs:
crazy_coins(5)
# => [[2, 1, 1]]
Successful output is supposed to resemble:
crazy_coins(5)
11
=> [2, 1, 1]
=> [[1, 0, 0], [0, 0, 0], [0, 0, 0]]
=> [[[0, 0, 0], 0, 0], [0, 0, 0], [0, 0, 0]]
What might be the best way to call coins.map! again on each element (recursively perhaps) until all coins[i][j] == 0?
I tried calling coins[0].map!(&generate) but the result is [[2, 1, 1], [2, 1, 1], [2, 1, 1]] Why is it not replacing the existing values with a new array?
returnstatement in a proc will return from the context in which the proc is called, not just the proc itself. You actually don't need an explicit return statement at all - just puttempas the last statement in the proc and it will be returned.