This week’s problem set deals with programming scripts that show what is the minimum amount of money a student has to pay in order to pay off his credit card balance in one year.

There are 12 months in a year, and each month, interest will be incurred on the balance, the updated balance after one month with interest incurred can be computed using the formula:

Updated balance each month = (Monthly unpaid balance) + (Monthly unpaid balance × Monthly interest rate)

Monthly unpaid balance = Balance − Monthly payment

Monthly interest rate = Annual interest rate / 12.0

Minimum monthly payment = Balance × Minimum monthly payment rate

**pset2.1.py**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
balance = 4842 annualInterestRate = 0.2 monthlyPaymentRate = 0.04 totalPaid = 0 for i in range(1, 13): print("Month: " + str(i)) minMonthlyPayment = round((balance * monthlyPaymentRate), 2) totalPaid += minMonthlyPayment print("Minimum monthly payment: " + str(minMonthlyPayment)) remainingBalance = balance - minMonthlyPayment balance = round((remainingBalance + remainingBalance * annualInterestRate / 12.0), 2) print("Remaining balance: " + str(balance)) print("Total paid: " + str(totalPaid)) print("Remaining balance: " + str(balance)) |

The first script shows the minimum monthly payment and remaining balance after each month, and at the end of one year, how much is paid total in one year and the remaining balance. The first script assumes that the student only pay minimum monthly payment each month.

**pset2.2.py**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
balance = 4123 annualInterestRate = 0.18 fixedMonthlyPayment = 0 b = balance a = annualInterestRate while True: # loop over each month of a year for i in range(1, 13): remainingBalance = balance - fixedMonthlyPayment balance = round((remainingBalance + remainingBalance * annualInterestRate / 12.0), 2) # if the balance is less than or equals to zero, print out value of fixedMonthlyPayment, break the loop if balance <= 0: print("Lowest Payment: " + str(fixedMonthlyPayment)) break else: # if the balance is not less than or equals to zero, increment fixedMonthlyPayment by 10, continue loop fixedMonthlyPayment += 10 balance = b annualInterestRate = a |

The second script shows how much money the student has to pay in order to clear the balance in one year. The output is a fixed amount that the student has to pay each month. This script uses the linear search (exhaustive enumeration) starting with 0 with step 10. This method is not so accurate because the step size is big, we cannot get an accurate answer, the answer is a multiple of 10.

**pset2.3.py**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
balance = 4123 annualInterestRate = 0.18 b = balance a = annualInterestRate lowerBound = balance / 12 upperBound = (balance * (1 + annualInterestRate / 12)** 12) / 12.0 fixedMonthlyPayment = round((lowerBound + upperBound) / 2, 2) while True: # loop over each month of a year for i in range(1, 13): remainingBalance = balance - fixedMonthlyPayment balance = round((remainingBalance + remainingBalance * annualInterestRate / 12.0), 2) # if the balance is near zero, print out value of fixedMonthlyPayment, break the loop if (abs(balance- 0)) < 0.1: print("Lowest Payment: " + str(round((fixedMonthlyPayment), 2))) break else: # if balance is less than zero, means fixedMonthlyPayment is too high if balance < 0: upperBound = fixedMonthlyPayment # if balance is more than zero, means fixedMonthlyPayment is too low elif balance > 0: lowerBound = fixedMonthlyPayment # try new fixedMonthlyPayment fixedMonthlyPayment = (lowerBound + upperBound) / 2 balance = b annualInterestRate = a |

The third script has the same purpose as the second script. However, this script uses the bisection search to find the fixed amount that the student has to pay each month. This is better than the second script because bisection search is fast, and can find answer accurate to few decimal places.

hi!

I figured out a wee shorter way to solve q3. check this out:

balance = 999999

annualInterestRate = 0.18

lower = balance/12

upper = (balance * (1 + annualInterestRate / 12)** 12) / 12.0

epsilon = 0.01

def payfunc(lower, upper):

payment =(lower+upper)/2

return payment

def duetopay(balance,annualInterestRate):

balance1 = balance

for month in range(0,12):

payment = payfunc(lower, upper)

balance1 = balance1 – payment

balance1 = balance1 + (annualInterestRate/12)*balance1

month += 1

return balance1

while abs(round(duetopay(balance,annualInterestRate),2)) > epsilon:

payment = payfunc(lower,upper)

balance1 = round(duetopay(balance,annualInterestRate),2)

if balance1 0:

lower = payment

print (‘Lowest Payment: ‘ + str(round(payment,2)))