So, as subject says, it is atm code that should return required sum using minimum amount of 100, 50 and 20 banknotes.
Just to simplify, the input is always positive; also the input is always divisible using mentioned banknotes nominals.
My question is not Python-specific (I know that my knowledge is weak, but I will learn it better soon), it is rather looking for proper algorithm. As far as I can see, looping is not proper way here. For example, it works fine with 290 input value; but in case of 110 it takes one 100 banknote, then 10 is left which doesn't have appropriate banknote; so it should skip 100 and return one 50 plus three 20.
Please advise if you have proper algorithm idea.
Code: Select all
def atm(value):
rest = value
result = ''
var100 = 0
var50 = 0
var20 = 0
if (rest - 100 >= 0):
while True:
rest = rest - 100
if (rest >= 0):
var100 = var100 + 1
else:
rest = rest + 100
break
if (rest - 50 >= 0):
while True:
rest = rest - 50
if (rest >= 0):
var50 = var50 + 1
else:
rest = rest + 50
break
if (rest - 20 >= 0):
while True:
rest = rest - 20
if (rest >= 0):
var20 = var20 + 1
else:
rest = rest + 20
break
result = str(var100) + ' ' + str(var50) + ' ' + str(var20)
return result
print (atm(290))