Daily Archives: April 6, 2016

edX_MITx_6.00.1x/Problem_Set_2/Paying_the_Minimum.py

def calculate_balance(balance, annualInterestRate, monthlyPaymentRate):

    totalPaid = 0
    remainingBalance = balance

    for month in range(1,13):
        monthlyInterestRate = annualInterestRate / 12.0
        minimumMonthlyPayment = round((monthlyPaymentRate * remainingBalance),2)
        totalPaid += minimumMonthlyPayment
        monthlyUpaidBalance = remainingBalance - minimumMonthlyPayment
        remainingBalance = round(monthlyUpaidBalance + (monthlyInterestRate * monthlyUpaidBalance),2)

        print("Month: {}".format(month))
        print("Minimum monthly payment: {}".format(minimumMonthlyPayment))
        print("Remaining Balance: {}".format(remainingBalance))

    print("Total paid: {}".format(totalPaid))
    print("Remaining Balance: {}".format(remainingBalance))


calculate_balance(balance, annualInterestRate, monthlyPaymentRate)

edX_MITx_6.00.1x/Problem_Set_2/Paying_Debt_Off_in_a_Year.py

def calculate_balance(balance, annualInterestRate):

    monthlyInterestRate = annualInterestRate / 12.0
    remainingBalance = balance
    minimumMonthlyPayment = 0

    while True:
        minimumMonthlyPayment += 10

        for _ in range(0,12):
            monthlyUpaidBalance = remainingBalance - minimumMonthlyPayment
            remainingBalance = round(monthlyUpaidBalance + (monthlyInterestRate * monthlyUpaidBalance),2)

        if remainingBalance < 0:
            break
        else:
            remainingBalance = balance

    print("Lowest Payment: {}".format(minimumMonthlyPayment))

calculate_balance(balance, annualInterestRate)

edX_MITx_6.00.1x/Problem_Set_2/Bisection_Search.py

def calculate_balance(balance, annualInterestRate):

    monthlyInterestRate = annualInterestRate / 12.0
    lowerBound = balance / 12
    upperBound = (balance * (1+ monthlyInterestRate)**12)/12
    minimumMonthlyPayment = (upperBound + lowerBound)/2.0

    while True:
        remainingBalance = balance

        for _ in range(0,12):
            monthlyUpaidBalance = remainingBalance - minimumMonthlyPayment
            remainingBalance = round(monthlyUpaidBalance + (monthlyInterestRate * monthlyUpaidBalance),2)

        if remainingBalance <= 0 and remainingBalance >= -0.01:
            break
        else:
            if remainingBalance > 0:
                lowerBound = minimumMonthlyPayment
            else:
                upperBound = minimumMonthlyPayment
        minimumMonthlyPayment = (upperBound + lowerBound)/2.0

    print("Lowest Payment: {}".format(round(minimumMonthlyPayment,2)))

calculate_balance(balance, annualInterestRate)