Python function: Find Change from purchase amount

倾然丶 夕夏残阳落幕 提交于 2019-12-02 13:32:02

Gee, you mean this isn't problem 2b in every programming course any more? Eh, probably not, they don't seem to teach people how to make change any more either. (Or maybe they do: is this a homework assignment?)

If you find someone over about 50 and have them make change for you, it works like this. Say you have a check for $3.52 and you hand the cashier a twnty. They'll make change by saying "three fifty-two" then

  • count back three pennies, saying "three, four, five" (3.55)
  • count back 2 nickels, (3.60, 3.65)
  • count back a dime (3.75)
  • a quarter (4 dollars)
  • a dollar bill (five dollars)
  • a $5 bill (ten dollars)
  • a $10 bill (twenty.)

That's at heart a recursive process: you count back the current denomination until the current amount plus the next denomination comes out even. Then move up to the next denomination.

You can, of course, do it iteratively, as above.

This is probably pretty fast - just a few operations per denomination:

def change(amount):
    money = ()
    for coin in [25,10,5,1]
        num = amount/coin
        money += (coin,) * num
        amount -= coin * num

    return money

Your best bet is to probably have a sorted dictionary of coin sizes, and then loop through them checking if your change is greater than the value, add that coin and subtract the value, otherwise move along to the next row in the dictionary.

Eg

Coins = [50, 25, 10, 5, 2, 1]
ChangeDue = 87
CoinsReturned = []
For I in coins:
   While I >= ChangeDue:
        CoinsReturned.add(I)
        ChangeDue = ChangeDue - I

Forgive my lousy python syntax there. Hope that's enough to go on.

This problem could be solved pretty easy with integer partitions from number theory. I wrote a recursive function that takes a number and a list of partitions and returns the number of possible combinations that would make up the given number.

http://sandboxrichard.blogspot.com/2009/03/integer-partitions-and-wiki-smarts.html

It's not exactly what you want, but it could be easily modified to get your result.

The above soloution working.

amount=int(input("Please enter amount in pence"))
coins = [50, 25, 10, 5, 2, 1]
coinsReturned = []
for i in coins:
  while amount >=i:
        coinsReturned.append(i)
        amount = amount - i
print(coinsReturned)

Alternatively a solution can reached by using the floor and mod functions.

amount = int(input( "Please enter amount in pence" ))
# math floor of 50
fifty = amount // 50
# mod of 50 and floor of 20
twenty = amount % 50 // 20
# mod of 50 and 20 and floor of 10
ten = amount % 50 % 20 // 10
# mod of 50 , 20 and 10 and floor of 5
five = amount % 50 % 20 % 10 // 5
# mod of 50 , 20 , 10 and 5 and floor of 2
two = amount % 50 % 20 % 10 % 5 // 2
# mod of 50 , 20 , 10 , 5 and 2 and floor of 1
one = amount % 50 % 20 % 10 % 5 % 2 //1

print("50p>>> " , fifty , " 20p>>> " , twenty , " 10p>>> " , ten , " 5p>>> " , five , " 2p>>> " , two , " 1p>>> " , one )

Or another solution

amount=int(input("Please enter the change to be given"))
endAmount=amount

coins=[50,25,10,5,2,1]
listOfCoins=["fifty" ,"twenty five", "ten", "five", "two" , "one"]
change = []

for coin in coins:
    holdingAmount=amount
    amount=amount//coin
    change.append(amount)
    amount=holdingAmount%coin

print("The minimum coinage to return from " ,endAmount, "p is as follows")
for i in range(len(coins)):
  print("There's " , change[i] ,"....",  listOfCoins[i] , "pence pieces in your change" )
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!