My code is like a mix of recursive and iterative and I'm returning the list coins[] when my B reached 0. But I'm always getting None, although my list coins is for this example [5, 1, 1, 1].
Maybe someone know what the problem is, because I'm very new to programming.
coins = []
def greedy_coin_change(M, B):
current_max = 0
if B == 0:
return coins
for value in M:
if value <= B:
if value > current_max:
current_max = value
B = B - current_max
coins.append(current_max)
if B != 0:
greedy_coin_change(M, B)
if B == 0:
return coins
print(greedy_coin_change([10, 5, 1], 8))
I got the solution, from a comment below. Just change it like that:
def greedy_coin_change(M, B):
.........
if B != 0:
return greedy_coin_change(M, B)
......