I'm curious why my approach to the change-making problem isn't succeeding. The logic makes sense to me, so I'm not sure where the failure is.
def count_change(denoms, denoms_length, amount):
"""
Finds the number of ways that the given number of cents can be represented.
:param denoms: Values of the coins available
:param denoms_length: Number of denoms
:param amount: Given cent
:return: The number of ways that the given number of cents can be represented.
"""
# Write your code here!
return helper(denoms, amount)
def helper(denoms, amount, i=0):
if amount == 0:
return 1
elif i >= len(denoms):
return 0
else:
count = 0
coin = denoms[i]
if coin < amount:
mult = amount // coin
for m in range(1, mult+1):
count += helper(denoms, amount - (coin*m), i+1)
else:
count += helper(denoms, amount, i+1)
return count
count_change([25, 10, 5, 1], 4, 30)
>>> 1 #should be 18
This problem is behind a paywall so I cannot link. The part that is most confusing to me is when a coin has several multiples that are less than the amount. This is the spirit behind the for loop in the else clause.
What am I doing wrong here?