I need some help because I think I'm lost. I've search before in this site and of course I've Google it, but believe me that if it was so simple to me, I haven't ask it at all, so please be kind to me. I'm new to python and coding isn't that easy to me.
Anyway, take a look at my code:
def coin_problem(price, cash_coins):
if (price < 0):
return []
if (price == 0):
return [[]]
options = []
for a_coin in cash_coins:
coins_minus_coin = cash_coins[:]
coins_minus_coin.remove(a_coin)
sub_coin_problem = coin_problem (price - a_coin, cash_coins)
for coin in sub_coin_problem:
coin.append(a_coin)
options.extend(sub_coin_problem)
return options
print coin_problem(4, [1, 2])
As you can see, I've tried to deal with the famous coin problem by recursion (and as I wrote before, I know many have already asked about this, I read their questions and the answers but I still couldn't understand the fully solutions).
This code was made by me, all of it. And now I'm stuck and confused. When the value of "price" is 4 and the value of "cash_coins" is [1,2] instead of returning something like this:
[1,1,1,1]
[2,2]
[2,1,1]
I get something more like:
[[1, 1, 1, 1], [2, 1, 1], [1, 2, 1], [1, 1, 2], [2, 2]]
the combination of "2" and the double "1" repeats 3 times instead of "1". I don't know what should I do in order to fix the problem or to improve my code so it will work better.
cash_coinsas both an iterable and a function.cash_problemsupposed to becoin_problem?