fork download
  1. #!/usr/bin/env python3
  2. _PERCENTS = 0.10, 0.15, 0.25, 0.28, 0.33, 0.35
  3. _THRESHOLDS = 8350, 33950, 82250, 171550, 372950
  4.  
  5.  
  6. def compute_tax(income):
  7. tax = 0
  8. for percent, threshold, prev_threshold in zip(_PERCENTS, _THRESHOLDS, (0,) + _THRESHOLDS):
  9. if income <= 0:
  10. break
  11. income_part = min(income, threshold - prev_threshold)
  12. tax += income_part * percent
  13. income -= income_part
  14.  
  15. if income > 0:
  16. tax += income * _PERCENTS[-1]
  17.  
  18. return tax
  19.  
  20.  
  21. def main():
  22. tax = compute_tax(5000.0)
  23. controlValue = 5000 * 0.10
  24. print('Tax is', (tax * 100) / 100.0)
  25. print('Control value is', (controlValue * 100) / 100.0)
  26.  
  27.  
  28. tax = compute_tax(10000.0)
  29. controlValue = 8350 * 0.10 + (10000 - 8350) * 0.15
  30. print('Tax is', tax * 100 / 100.0)
  31. print('Control value is', (controlValue * 100) / 100.0)
  32.  
  33.  
  34. tax = compute_tax(50000.0)
  35. controlValue = 8350 * 0.10 + (33950 - 8350) * 0.15 + \
  36. (50000 - 33950) * 0.25
  37. print('Tax is', (tax * 100) / 100.0)
  38. print('Control value is', (controlValue * 100) / 100.0)
  39.  
  40.  
  41. tax = compute_tax(100000.0)
  42. controlValue = 8350 * 0.10 + (33950 - 8350) * 0.15 + \
  43. (82250 - 33950) * 0.25 + (100000 - 82250) * 0.28
  44. print('Tax is', (tax * 100) / 100.0)
  45. print('Control value is', (controlValue * 100) / 100.0)
  46.  
  47.  
  48. tax = compute_tax(200000.0)
  49. controlValue = 8350 * 0.10 + (33950 - 8350) * 0.15 + \
  50. (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + \
  51. (200000 - 171550) * 0.33
  52. print('Tax is', (tax * 100) / 100.0)
  53. print('Control value is', (controlValue * 100) / 100.0)
  54.  
  55.  
  56. tax = compute_tax(500000.0)
  57. controlValue = 8350 * 0.10 + (33950 - 8350) * 0.15 + \
  58. (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + \
  59. (372950 - 171550) * 0.33 + (500000 - 372950) * 0.35
  60. print('Tax is', (tax * 100) / 100.0)
  61. print('Control value is', (controlValue * 100) / 100.0)
  62.  
  63. if __name__ == '__main__':
  64. main()
Success #stdin #stdout 0.02s 9120KB
stdin
Standard input is empty
stdout
Tax is 500.0
Control value is 500.0
Tax is 1082.5
Control value is 1082.5
Tax is 8687.5
Control value is 8687.5
Tax is 21720.0
Control value is 21720.0
Tax is 51142.5
Control value is 51142.5
Tax is 152683.5
Control value is 152683.5