Advertisement
Guest User

asdf

a guest
Jul 17th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. import math
  2.  
  3. def splitIntoBills(money, denominations=None):
  4.     denominations = denominations or {100: -1, 50: -1, 20: -1, 10: -1, 5: -1, 1: -1}
  5.     billsPerDenom = {}
  6.    
  7.     for i in sorted(denominations, reverse=True):
  8.         denom, billsLeft = i, denominations[i]
  9.        
  10.         bills = math.floor(money / denom)
  11.         if billsLeft >= 0: bills = min(bills, billsLeft)
  12.        
  13.         billsPerDenom[denom] = bills
  14.         money -= (bills * denom)
  15.    
  16.     billsPerDenom["left"] = money
  17.     return billsPerDenom
  18.  
  19.  
  20. # >>> splitIntoBills(842)
  21. # {100: 8, 50: 0, 20: 2, 10: 0, 5: 0, 1: 2, 'left': 0}
  22. #
  23. # >>> splitIntoBills(842, {100: 3, 50: -1, 20: -1, 10: -1, 5: -1, 1: -1})
  24. # {100: 3, 50: 10, 20: 2, 10: 0, 5: 0, 1: 2, 'left': 0}
  25. #
  26. # >>> splitIntoBills(842, {20: -1})
  27. # {20: 42, 'left': 2}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement