This is a dynamic programming solution to the minimum change problem in python from Bryce Boe. I don't understand what's going on in the elif block in general. More specifically, what is the [:] doing in this line
table[i] = table[i - coin][:]
Could this be written with a dictionary instead of the table memoization list?
def solve_coin_change(coins, value):
"""A dynamic solution to the coin change problem"""
table = [None for x in range(value + 1)]
table[0] = []
for i in range(1, value + 1):
for coin in coins:
if coin > i:
continue
elif not table[i] or len(table[i - coin]) + 1 < len(table[i]):
if table[i - coin] != None:
table[i] = table[i - coin][:]
table[i].append(coin)
if table[-1] != None:
print('%d coins: %s' % (len(table[-1]), table[-1]))
else:
print('No solution possible')
coins = [1, 2, 4]
value = 11
solve_coin_change(coins, value)