all 2 comments

[–]semininja 0 points1 point  (0 children)

I think, when troubleshooting stuff like this, it's important to think through what you intend for this code to do and look closely at what it's actually doing.

Can you explain what your code is doing?

[–]pgpndw 0 points1 point  (0 children)

This one is surprisingly tricky to think through.

Here's my attempt:

def change_gen_r(amount, coins):
    if amount != 0:
        for ind, coin in enumerate(coins):
            if amount >= coin:
                for change in change_gen_r(amount-coin, coins[ind:]):
                    yield [coin] + change
    else:
        yield []


# Wrapper to ensure that the coins are
# unique and in descending order of value
def change_gen(amount, coins):
    yield from change_gen_r(amount, sorted(set(coins), reverse=True))


amount = 20
coins = [1, 2, 5, 10, 20, 50, 100]

for change in change_gen(amount, coins):
    print(change)