[Solved] Python finding most efficient combination of coins to reach item value


For this to work, you would have to sort your coins in descending order, or use reversed(coinList) instead of coinList in the first for loop and vice versa in the second if you would like to sort them forwards:

    total = 250
    value = 0
    coins = []
    coinList = [100, 100, 20, 20, 20, 10, 10]
    for coin in coinList:
        if value + coin <= total:
            value += coin
            coins.append(coin)
        else:
            continue
        if value >= total:
            break
    else:
        for coin in reversed(coinList):
            value += coin
            coins.append(coin)
            if value >= total:
                break
    print coins

2

solved Python finding most efficient combination of coins to reach item value