Python function: Find Change from purchase amount

前端 未结 6 775
旧时难觅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
    

提交回复
热议问题