# Reference:
# runestone.academy/runestone/books/published/pythonds/Recursion/DynamicProgramming.html
def solve(money, amount, counts, answer):
for value in range(amount + 1):
count = value # 1 must be the smallest face value in money
last = 1
for j in money:
if j > value: # money must be sorted
break
if counts[value - j] + 1 < count:
count = counts[value-j] + 1
last = j
counts[value] = count
answer[value] = last
return counts[amount]
def display(amount, counts, answer):
last = amount
values = []
while last != 0:
values.append(answer[last])
last -= answer[last]
unique = sorted(set(values))
print('count: {} ({})'.format(counts[amount], len(unique)))
print('values:')
for x in unique:
print(' {} x {}'.format(x, values.count(x)))
def main():
amount = 6789
money = sorted([5000, 1000, 100, 50, 20, 10, 5, 2, 1])
counts = [0] * (amount + 1)
answer = counts[:]
solve(money, amount, counts, answer)
display(amount, counts, answer)
if __name__ == '__main__':
main()
Comments
Leave a comment