Advertisement
libdo

Untitled

Sep 21st, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. """
  2. Problem #1
  3. """
  4.  
  5. balance = 42
  6. annualInterestRate = 0.2
  7. monthlyPaymentRate = 0.04
  8.  
  9. def cc_12mo_bal(balance, annualInterestRate, monthlyPaymentRate):
  10. """
  11.  
  12. Write a program to calculate the credit card balance after
  13. one year if a person only pays the minimum monthly
  14. payment required by the credit card company each month.
  15.  
  16. """
  17. Monthly_Int_Rate = annualInterestRate / 12
  18. Min_Monthly_Pmt = monthlyPaymentRate * balance
  19. Monthly_Unpaid_Bal = balance - Min_Monthly_Pmt
  20. Updated_bal = Monthly_Unpaid_Bal + (Monthly_Int_Rate * Monthly_Unpaid_Bal)
  21. if hasattr(cc_12mo_bal, "count"):
  22. cc_12mo_bal.count += 1
  23. else:
  24. cc_12mo_bal.count = 1
  25. if cc_12mo_bal.count == 12:
  26. return print("Remaining balance: " + str(round(Updated_bal, 2)))
  27. else:
  28. return cc_12mo_bal(Updated_bal, annualInterestRate, monthlyPaymentRate)
  29.  
  30. cc_12mo_bal(balance, annualInterestRate, monthlyPaymentRate)
  31.  
  32. """
  33. Problem #2
  34. """
  35.  
  36. def cc_12mo_payoff(balance, annualInterestRate, \
  37. increment = 10, epsilon = 10, \
  38. guess = 10, \
  39. num_guesses = 12):
  40. """
  41.  
  42. Write a program that calculates the minimum fixed monthly payment
  43. needed in order pay off a credit card balance within 12 months.
  44. By a fixed monthly payment, we mean a single number which does
  45. not change each month, but instead is a constant amount that will
  46. be paid each month.
  47.  
  48. """
  49.  
  50. def pmt_trial(balance, annualInterestRate, num_guesses, \
  51. guess = round((balance*(1+annualInterestRate/12)**12)*.0833333,-1)):
  52. Monthly_Int_Rate = annualInterestRate / 12
  53. Monthly_Unpaid_Bal = balance - guess
  54. Updated_bal = Monthly_Unpaid_Bal + (Monthly_Int_Rate * Monthly_Unpaid_Bal)
  55. if num_guesses == 1:
  56. return Updated_bal
  57. else:
  58. return pmt_trial(Updated_bal, annualInterestRate, num_guesses - 1, guess)
  59.  
  60. Updated_bal = pmt_trial(balance, annualInterestRate, num_guesses, guess)
  61.  
  62. if Updated_bal <= 0:
  63. print("Lowest Payment: " + str(guess))
  64. else:
  65. return cc_12mo_payoff(balance, annualInterestRate, guess = guess + increment)
  66.  
  67. cc_12mo_payoff(balance, annualInterestRate)
  68.  
  69.  
  70. """
  71. Problem #3
  72. """
  73.  
  74. def cc_12mo_payoff(balance, annualInterestRate, lower_bound = 0, \
  75. upper_bound = 0, guess = 10, num_guesses = 12, epsilon = .01):
  76. """
  77.  
  78. Write a program that uses these bounds and bisection search (for more info
  79. check out the Wikipedia page on bisection search) to find the smallest
  80. monthly payment to the cent (no more multiples of $10) such that we can
  81. pay off the debt within a year. Try it out with large inputs, and notice
  82. how fast it is (try the same large inputs in your solution to Problem 2 to
  83. compare!). Produce the same return value as you did in Problem 2.
  84.  
  85. """
  86. Monthly_Int_Rate = annualInterestRate / 12
  87.  
  88. if lower_bound == 0 and upper_bound == 0:
  89. lower_bound = balance / 12
  90. upper_bound = balance * ((1+Monthly_Int_Rate)**12)/12.0
  91. guess = (upper_bound + lower_bound) / 2
  92. else:
  93. guess = (upper_bound + lower_bound) / 2
  94.  
  95. def pmt_trial(balance, annualInterestRate, num_guesses, guess):
  96. Monthly_Unpaid_Bal = balance - guess
  97. Updated_bal = Monthly_Unpaid_Bal + \
  98. (Monthly_Int_Rate * Monthly_Unpaid_Bal)
  99. if num_guesses == 1:
  100. return Updated_bal
  101. else:
  102. return pmt_trial(Updated_bal, annualInterestRate, \
  103. num_guesses - 1, guess)
  104.  
  105. Updated_bal = pmt_trial(balance, annualInterestRate, num_guesses, guess)
  106.  
  107. if Updated_bal <= 0 and Updated_bal >= -0.01:
  108. print("Lowest Payment: " + str(round(guess,2)))
  109. elif Updated_bal > 0:
  110. return cc_12mo_payoff(balance, annualInterestRate,
  111. lower_bound = guess, upper_bound = upper_bound)
  112. else:
  113. return cc_12mo_payoff(balance, annualInterestRate, \
  114. lower_bound = lower_bound, upper_bound = guess)
  115.  
  116. cc_12mo_payoff(999999, 0.18)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement