I was doing a coding challenge and run into this problem that I have never encounter before. The question ask to return change in the most efficient way. User put in the price, and the amount paid as an array (kinda like giving money to the vending machine)
for some reason, the code doesn't pass the last if block when the remaining change is 0.01
def ChangeMaker(price, payment):
# Write your code here
totalPayment=0
for i in payment:
totalPayment+=i
change = totalPayment-price
coinChange=change-int(change)
def helper(remain, result):
holder= result
newRemain = remain
if remain>=.25:
holder[3]+=1
newRemain-=.25
return helper(newRemain, holder)
elif remain>=.10:
holder[2]+=1
newRemain -=.10
return helper(newRemain, holder)
elif remain>=.5:
holder[1]+=1
newRemain -=.05
return helper(newRemain, holder)
elif remain>.01 or remain==.01:
holder[0]+=1
newRemain -=.01
return helper(newRemain, holder)
else:
return holder;
return helper(coinChange, [0,0,0,0])
print(ChangeMaker(1.87, [5, .25,.25,.25,.25, 1]))