This week’s problem set involved **calculating credit card payments and interest rates over time**. The objective comes with three sub-tasks: calculating payments over a fixed period, finding the amount required to pay debt in 12 months through exhaustive enumeration, and improving this second task using binary search.

concepts covered in lecture:

- Exhaustive enumeration often the right solution
- Break exits only the current loop
- If the answer falls outside the range tested, answer won’t be found
- E.g. range(0, 100) when answer is negative

## Paying the Minimum

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 28 29 30 31 32 33 34 35 36 37 |
beginning_balance = 0 annual_interest_rate = 0 monthly_minimum_rate = 0 paid = 0 # don't forget to convert these from strings beginning_balance = raw_input("What is your beginning balance?\n") annual_interest_rate = raw_input("What is your annual interest rate as \ decimal?\n") monthly_minimum_rate = raw_input("What is the monthly minimum rate as \ decimal?\n") # really should use decimal not float beginning_balance = float(beginning_balance) annual_interest_rate = float(annual_interest_rate) monthly_minimum_rate = float(monthly_minimum_rate) paid = float(paid) # calculate how much will be paid over 12 months for i in range(1, 13): print "Month: ", i print "Beginning balance: $%.2f" % beginning_balance monthly_minimum_payment = round(float(monthly_minimum_rate * beginning_balance), 2) print "Minimum monthly payment: $%.2f" % monthly_minimum_payment # track how much was paid paid += monthly_minimum_payment principle_paid = round(float(monthly_minimum_rate * beginning_balance - annual_interest_rate / 12 * beginning_balance), 2) print "Principle paid: $%.2f" % principle_paid # calculate new balance beginning_balance = round(float(beginning_balance - principle_paid), 2) print "Remaining balance: $%.2f" % beginning_balance print "TOTAL" print "Total amount paid: $", paid print "Remaining balance: $", beginning_balance |

## Paying off debt in 12 months

### (brute force method)

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 28 29 30 31 32 33 34 35 36 37 38 39 |
# Write a program that calculates the minimum fixed monthly payment # needed in order to pay off a debt within 12 months. # Assume the interest is compounded monthly. # The monthly payment must be a mutiple of $10 and same for every month import math import decimal balance = raw_input("What is the total balance on your debt?\n") balance = float(balance) yearly_interest = raw_input("What is the interest rate as decimal?\n") yearly_interest = float(yearly_interest) monthly_interest = yearly_interest / 12 proposed_payment = 10 print balance, " ", type(balance) print yearly_interest, " ", type(yearly_interest) print monthly_interest, " ", type(monthly_interest) balance_reset = balance time = 0 paid_reset = 0 while balance > 0: balance = balance_reset paid = paid_reset for i in range(1, 13): balance = balance + balance * yearly_interest / 12 print "Month: %d" % i print "Your balance: $%.2f" % balance print "Your payment: $%d" % proposed_payment balance = balance - proposed_payment time = i paid += proposed_payment proposed_payment += 10 print "Paying $%.2f per month will pay your debt in %d" % (proposed_payment - 10, time) print "Final balance: $%.2f" % balance print "TOTAL PAID: $%.2f" % paid |

### (binary search method)

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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# Write the same program with increments of $0.01. # Find the monthly payment using bounds and bisection search. # The monthly payment must be the same for every month. import math import decimal balance = raw_input("What is the total balance on your debt?\n") balance = float(balance) yearly_interest = raw_input("What is the interest rate as decimal?\n") yearly_interest = float(yearly_interest) monthly_interest = yearly_interest / 12 proposed_min = round(balance / 12, 2) proposed_max = round((balance * (1 + monthly_interest) ** 12) / 12) proposed_payment = float(proposed_min + proposed_max / 2) print balance, " ", type(balance) print yearly_interest, " ", type(yearly_interest) print monthly_interest, " ", type(monthly_interest) balance_reset = balance steps = 0 time = 0 paid_reset = 0 while balance < -0.04 or balance > 0.04: balance = balance_reset paid = paid_reset steps += 1 for i in range(1, 13): balance = balance + balance * monthly_interest #print "Month: %d" % i #print "Your balance: $%.2f" % balance #print "Your payment: $%.2f" % proposed_payment balance = balance - proposed_payment time = i paid += proposed_payment print "Beginning balance was: ", balance_reset print "Remaining balance was: ", balance print "Proposed payment was: ", proposed_payment if balance > 0.04: proposed_min = proposed_payment proposed_payment = (proposed_min + proposed_max) / 2 else: proposed_max = proposed_payment proposed_payment = (proposed_min + proposed_max) / 2 print "Paying $%.2f per month will pay your debt in %d months." % ( proposed_payment - 0.01, time) print "Final balance: $%.2f" % balance print "Number of iterations: ", steps print "TOTAL PAID: $%.2f" % paid |

If you made it this far, thank you for reading. I am aware I should be using decimal types, no floats, for much of the calculation here. I spent too much time trying to get decimal to work and failing, so I used float formatting and moved on.

Critiques and questions are appreciated.