2014-04-25 23:59:14 +00:00
|
|
|
def change(n, list_of_coins)
|
2014-11-14 08:57:29 +00:00
|
|
|
change_helper(n, list_of_coins, [])
|
2014-04-25 23:59:14 +00:00
|
|
|
end
|
|
|
|
|
2014-11-14 08:57:29 +00:00
|
|
|
def change_helper(n, list_of_coins, result_list)
|
2014-04-25 23:59:14 +00:00
|
|
|
if list_of_coins.empty?
|
2014-11-14 08:57:29 +00:00
|
|
|
return []
|
2014-04-25 23:59:14 +00:00
|
|
|
elsif n == 0
|
2014-11-14 08:57:29 +00:00
|
|
|
return [result_list]
|
2014-04-25 23:59:14 +00:00
|
|
|
else
|
2014-11-14 08:57:29 +00:00
|
|
|
list_of_coins.each do |coin|
|
|
|
|
if n < coin
|
|
|
|
return []
|
|
|
|
else
|
|
|
|
return change_helper(n - coin, list_of_coins, result_list + [coin]) + change_helper(n, list_of_coins - [coin], result_list)
|
|
|
|
end
|
2014-04-25 23:59:14 +00:00
|
|
|
end
|
|
|
|
end
|
2014-11-14 08:57:29 +00:00
|
|
|
|
2014-04-25 23:59:14 +00:00
|
|
|
end
|