Minimum Coin change problem – Backtracking

Question:

I’m trying to solve Coin change problem with minimum number of coins
using backtracking method.

I’ve actually done with it, but I want to add some option that print the number of coins by its unit not only total amount.

this is my python codes below.

def minimum_coins(coin_list, change):
min_coins = change 
if change in coin_list: 
    return 1
else:
    for coin in coin_list:
        if coin < change: 
            num_coins = 1 + minimum_coins(coin_list, change - coin)
            if num_coins < min_coins:
                min_coins = num_coins
return min_coins

coin_list = []
unit = input("Enter the Coin Unitn")
#1 10 15 20
change = int(input("Enter the Total Changen"))
#73
unit = unit.split()

for i in unit:
    coin_list.append(int(i))

min = minimum_coins(coin_list, change)
#print("You need 3 1's, 1 10's, 0 15's, 3 20's") <- I want to implement this option.
print("Total number of coins required is %s." % min) #7

I’m struggling to implement this in my code.

cause back-tracking is complex, I can’t get the idea how to check the number of coins by its unit.

Asked By: rookie_pitcher

||

Answers:

Possible way:

def minimum_coins(coin_list, change):
    min_coins = change
    if change in coin_list:
        return 1, [change]
    else:
        cl = []
        for coin in coin_list:
            if coin < change:
                mt, t = minimum_coins(coin_list, change - coin)
                num_coins = 1 + mt
                if num_coins < min_coins:
                    min_coins = num_coins
                    cl = t + [coin]
    return min_coins, cl

change = 73
coin_list = [1, 10, 15, 20]
min, c = minimum_coins(coin_list, change)
#print("You need 3 1's, 1 10's, 0 15's, 3 20's") <- I want to implement this option.
print("Total number of coins required is %s." % min, c) #7

>>>Total number of coins required is 7. [20, 20, 20, 10, 1, 1, 1]
Answered By: MBo

# create dictionary from list of notes

#i am newbie in python

#the code is not mine, i just found it on the net

notes = [ 200, 100, 50, 20, 10, 5, 1]
notesCount = {}
x=122

for note in notes:

    if x >= note:
       notesCount[note] = x//note
       x=x%note
print(notesCount)
values = notesCount.values()
print(values)
total = sum(values)
print(total)
Answered By: osama
give_back = 122
return_bills = []
bill_stock = [ 
    { "bill_type":200,    "instock":1 },
    { "bill_type":100,    "instock":1 },
    { "bill_type":50,     "instock":1 },
    { "bill_type":20,     "instock":1 },
    { "bill_type":10,     "instock":1 },
    { "bill_type":5,      "instock":1 },
    { "bill_type":1,      "instock":1 },
]

for item in bill_stock:
    # return when give back is 0
    if give_back == 0: break

    # get maximum amount to give back on the highest bill type
    qty = give_back // item["bill_type"]
    qty = min(qty, item["instock"])

    if qty > 0:
        give_back -= (item["bill_type"] * qty)
        return_bills.append({
            "bill_type": item["bill_type"],
            "qty": qty
        })

# display what to give back
for item in return_bills:
    quantity = item["qty"]
    bill_type = item["bill_type"]
    print(f"{quantity}X ---- {bill_type}")

# output:
# > py .calc.py
# 1X ---- 100
# 1X ---- 20
# 1X ---- 1
Answered By: niek tuytel