19 lines
513 B
Ruby
19 lines
513 B
Ruby
def change(n, list_of_coins)
|
|
changeHelper(n, list_of_coins, [])
|
|
end
|
|
|
|
def changeHelper(n, list_of_coins, result_list)
|
|
if list_of_coins.empty?
|
|
[result_list]
|
|
elsif n == 0
|
|
[result_list]
|
|
else
|
|
if n >= list_of_coins.first
|
|
changeHelper(n - list_of_coins.first, list_of_coins, result_list << list_of_coins.first) +
|
|
changeHelper(n, list_of_coins[1..list_of_coins.count-1], result_list)
|
|
else
|
|
changeHelper(n, list_of_coins[1..list_of_coins.count-1], result_list)
|
|
end
|
|
end
|
|
end
|