forked from jamil-said/code-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoinChange.py
More file actions
executable file
·31 lines (26 loc) · 1.14 KB
/
coinChange.py
File metadata and controls
executable file
·31 lines (26 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
""" coinChange
Given a coin list and a desired change, calculate the minimum amount of
coins to give to make the exact change, and also return the coins used.
Assume that it is always possible to make the change (or one of the coins
is 1 cent)
"""
def coinChange(coinList,change):
minCoins, coinsTable = [0]*(change+1), [0]*(change+1)
for chng in range(change+1):
coinNumber = chng
newCoin = coinList[0]
for coinUnit in [c for c in coinList if c <= chng]:
if minCoins[chng-coinUnit] + 1 < coinNumber:
coinNumber = minCoins[chng-coinUnit] + 1
newCoin = coinUnit
minCoins[chng], coinsTable[chng] = coinNumber, newCoin
coinsUsed, chngLeft = [], change
while chngLeft > 0:
thisCoin = coinsTable[chngLeft]
coinsUsed.append(thisCoin)
chngLeft = chngLeft - thisCoin
return (minCoins[change], coinsUsed)
# assuming that coinList (1st argument) is ordered
print(coinChange([1,5,10,21,25],63)) #(3, [21, 21, 21])
print(coinChange([1,5,10,21,25],17)) #(4, [1, 1, 5, 10])
print(coinChange([1,5,10,21,25],178)) #(9, [1, 10, 21, 21, 25, 25, 25, 25, 25])