0

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.

11
  • 2
    I dont see the recursion, but I do see you using cash_coins as both an iterable and a function. Commented Nov 8, 2013 at 16:46
  • cmd: Indeed, my mistake. check again. What about now? Mr E: again, my mistake. check again. Commented Nov 8, 2013 at 16:49
  • Is cash_problem supposed to be coin_problem? Commented Nov 8, 2013 at 16:50
  • Indeed. Sorry about all my mistakes... hope it's my last one. Commented Nov 8, 2013 at 16:51
  • 1
    On StackOverflow we don't delete questions when they're answered - we leave them up so other people can learn from them too. You can delete it yourself if you really want, but please consider leaving it. Commented Nov 8, 2013 at 18:56

2 Answers 2

2

When you want to add a single item to a list, use append. When you want to combine two lists together, use extend.

Sign up to request clarification or add additional context in comments.

Comments

0

Tips:

    coins_minus_coin = cash_coins[:]
    coins_minus_coin.remove(coin)

You never use this variable.

    for i in sub_coins:
        i.append(coin)
    cash_coins_options.append(sub_coins)

You never use i either. I'd guess you meant:

    for i in sub_coins:
        i.append(coin)
        cash_coins_options.append(i)

That solves the problem of stange results, but your solution will still only find []. Why? Your recursion can only stop on a return []; it can't handle another fundamental case when you can tell the price using a single coin. Try adding at the top this simple condition:

# actually I changed my mind-
# I assume you're learning so try this one yourself :-)

This will cause your function to behave much better:

>>> print coin_problem(4, [1,2])
[[2, 1, 1], [1, 2, 1], [2, 2]]

which manages to produce correct answers (even though it duplicates some of them).

1 Comment

I liked it more before :-) You mostly renamed variables, I did the same with your new version and got the same result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.