Matt23

Salary payment algorithm

Dec 21st, 2015
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. import math
  2.  
  3. salaries = [4352, 4536, 3224, 7686, 1243, 659, 633, 1324, 1254, 1678, 1160.64]
  4.  
  5. payments = [[0 for x in range(15)] for x in range(len(salaries))]
  6. banknotes = [200, 100, 50, 20, 10, 5, 2, 1]
  7. coines = [50, 20, 10, 5, 2, 1]
  8.  
  9. def doJob(i, salary):
  10.     cents = 0
  11.     if salary % 1 is not salary:
  12.         cents = int((salary * 100) % 100)
  13.         salary = int(math.floor(salary))
  14.  
  15.     for j, banknote in enumerate(banknotes):
  16.         if salary >= banknote:
  17.             payments[i][j + 1] = int(math.floor(salary / banknote))
  18.             salary = salary % banknote
  19.     for j, coin in enumerate(coines):
  20.         if cents >= coin:
  21.             payments[i][j + 9] = int(math.floor(cents / coin))
  22.             cents = cents % coin
  23.  
  24. for i, salary in enumerate(salaries):
  25.     payments[i][0] = salary
  26.     doJob(i, salary)
  27.  
  28. toPrint = ""
  29. for payment in payments:
  30.     toPrint += str(payment[0]) + ": "
  31.  
  32.     for i, value in enumerate(payment[1:9]):
  33.         if value is not 0:
  34.             toPrint += "{0} x{1}, ".format(banknotes[i], value)
  35.     for i, value in enumerate(payment[9:]):
  36.         if value is not 0:
  37.             toPrint += "0.{0} x{1}, ".format(coines[i], value)
  38.  
  39.     toPrint += '\n'
  40. print(toPrint)
Advertisement
Add Comment
Please, Sign In to add comment