Returning a list in this recursive coi function in python.

Posted by Nate on Stack Overflow See other posts from Stack Overflow or by Nate
Published on 2010-04-03T18:57:57Z Indexed on 2010/04/03 19:03 UTC
Read the original article Hit count: 136

Filed under:
|
|

Hello. I'm having trouble getting my list to return in my code. Instead of returning the list, it keeps returning None, but if I replace the return with print in the elif statement, it prints the list just fine. How can I repair this?

def makeChange2(amount, coinDenomination, listofcoins = None):
#makes a list of coins from an amount given by using a greedy algorithm
    coinDenomination.sort()
    #reverse the list to make the largest position 0 at all times
    coinDenomination.reverse()
    #assigns list
    if listofcoins is None:
        listofcoins = []
    if amount >= coinDenomination[0]:
        listofcoins = listofcoins + [coinDenomination[0]]
        makeChange2((amount - coinDenomination[0]), coinDenomination, listofcoins)
    elif amount == 0:
        return listofcoins
    else:

        makeChange2(amount, coinDenomination[1:], listofcoins)

© Stack Overflow or respective owner

Related posts about python

Related posts about recursion