euler/euler031.rb

21 lines
449 B
Ruby
Raw Normal View History

2014-04-25 23:59:14 +00:00
def change(n, list_of_coins)
change_helper(n, list_of_coins, [])
2014-04-25 23:59:14 +00:00
end
def change_helper(n, list_of_coins, result_list)
2014-04-25 23:59:14 +00:00
if list_of_coins.empty?
return []
2014-04-25 23:59:14 +00:00
elsif n == 0
return [result_list]
2014-04-25 23:59:14 +00:00
else
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-04-25 23:59:14 +00:00
end