Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Problem #1
- """
- balance = 42
- annualInterestRate = 0.2
- monthlyPaymentRate = 0.04
- def cc_12mo_bal(balance, annualInterestRate, monthlyPaymentRate):
- """
- Write a program to calculate the credit card balance after
- one year if a person only pays the minimum monthly
- payment required by the credit card company each month.
- """
- Monthly_Int_Rate = annualInterestRate / 12
- Min_Monthly_Pmt = monthlyPaymentRate * balance
- Monthly_Unpaid_Bal = balance - Min_Monthly_Pmt
- Updated_bal = Monthly_Unpaid_Bal + (Monthly_Int_Rate * Monthly_Unpaid_Bal)
- if hasattr(cc_12mo_bal, "count"):
- cc_12mo_bal.count += 1
- else:
- cc_12mo_bal.count = 1
- if cc_12mo_bal.count == 12:
- return print("Remaining balance: " + str(round(Updated_bal, 2)))
- else:
- return cc_12mo_bal(Updated_bal, annualInterestRate, monthlyPaymentRate)
- cc_12mo_bal(balance, annualInterestRate, monthlyPaymentRate)
- """
- Problem #2
- """
- def cc_12mo_payoff(balance, annualInterestRate, \
- increment = 10, epsilon = 10, \
- guess = 10, \
- num_guesses = 12):
- """
- Write a program that calculates the minimum fixed monthly payment
- needed in order pay off a credit card balance within 12 months.
- By a fixed monthly payment, we mean a single number which does
- not change each month, but instead is a constant amount that will
- be paid each month.
- """
- def pmt_trial(balance, annualInterestRate, num_guesses, \
- guess = round((balance*(1+annualInterestRate/12)**12)*.0833333,-1)):
- Monthly_Int_Rate = annualInterestRate / 12
- Monthly_Unpaid_Bal = balance - guess
- Updated_bal = Monthly_Unpaid_Bal + (Monthly_Int_Rate * Monthly_Unpaid_Bal)
- if num_guesses == 1:
- return Updated_bal
- else:
- return pmt_trial(Updated_bal, annualInterestRate, num_guesses - 1, guess)
- Updated_bal = pmt_trial(balance, annualInterestRate, num_guesses, guess)
- if Updated_bal <= 0:
- print("Lowest Payment: " + str(guess))
- else:
- return cc_12mo_payoff(balance, annualInterestRate, guess = guess + increment)
- cc_12mo_payoff(balance, annualInterestRate)
- """
- Problem #3
- """
- def cc_12mo_payoff(balance, annualInterestRate, lower_bound = 0, \
- upper_bound = 0, guess = 10, num_guesses = 12, epsilon = .01):
- """
- Write a program that uses these bounds and bisection search (for more info
- check out the Wikipedia page on bisection search) to find the smallest
- monthly payment to the cent (no more multiples of $10) such that we can
- pay off the debt within a year. Try it out with large inputs, and notice
- how fast it is (try the same large inputs in your solution to Problem 2 to
- compare!). Produce the same return value as you did in Problem 2.
- """
- Monthly_Int_Rate = annualInterestRate / 12
- if lower_bound == 0 and upper_bound == 0:
- lower_bound = balance / 12
- upper_bound = balance * ((1+Monthly_Int_Rate)**12)/12.0
- guess = (upper_bound + lower_bound) / 2
- else:
- guess = (upper_bound + lower_bound) / 2
- def pmt_trial(balance, annualInterestRate, num_guesses, guess):
- Monthly_Unpaid_Bal = balance - guess
- Updated_bal = Monthly_Unpaid_Bal + \
- (Monthly_Int_Rate * Monthly_Unpaid_Bal)
- if num_guesses == 1:
- return Updated_bal
- else:
- return pmt_trial(Updated_bal, annualInterestRate, \
- num_guesses - 1, guess)
- Updated_bal = pmt_trial(balance, annualInterestRate, num_guesses, guess)
- if Updated_bal <= 0 and Updated_bal >= -0.01:
- print("Lowest Payment: " + str(round(guess,2)))
- elif Updated_bal > 0:
- return cc_12mo_payoff(balance, annualInterestRate,
- lower_bound = guess, upper_bound = upper_bound)
- else:
- return cc_12mo_payoff(balance, annualInterestRate, \
- lower_bound = lower_bound, upper_bound = guess)
- cc_12mo_payoff(999999, 0.18)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement