Python function: Find Change from purchase amount

前端 未结 6 776
旧时难觅i
旧时难觅i 2021-01-29 13:56

I\'m looking for the most efficient way to figure out a change amount (Quarters, dimes, nickels, and pennies) from a purchase amount. The purchase amount must be less than $1, a

相关标签:
6条回答
  • 2021-01-29 14:09

    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
    
    0 讨论(0)
  • 2021-01-29 14:15

    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.

    0 讨论(0)
  • 2021-01-29 14:18

    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" )
    
    0 讨论(0)
  • 2021-01-29 14:18

    I have an improve solution from above solutions

    coins=[]
    cost = float(input('Input the cost: '))
    give = float(input('Tipe Amount given: '))
    
    change = (give - cost)
    change2 = change-int(change)
    change2 = round(change2,2)*100
    coin = [50,25,10,5,1]
    for c in coin:
        while change2>=c:
            coins.append(c)
            change2 = change2-c
        
    half=coins.count(50)
    qua = coins.count(25)
    dime=coins.count(10)
    ni=coins.count(5)
    pen=coins.count(1)
    dolars = int(change)
    if half !=0 or qua != 0 or dime!=0 or ni!=0 or pen!=0:
        print ('The change of', round(give,2), 'is:',change, 'like \n-dolas:',dolars,'\n-halfs:',half,'\n-quarters:',qua,'\n-dime:',dime,'\n-nickels:',ni,'\n-pennies:',pen)
    else:
         print ('The change from', round(give,2), 'is:',change,'and no coins')
    
    0 讨论(0)
  • 2021-01-29 14:29

    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.

    0 讨论(0)
  • 2021-01-29 14:32

    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.

    0 讨论(0)
提交回复
热议问题