Celauran


Joined 2 years ago
Homeworks submitted:
Homework comments:
1
0

About Me

No description provided.

Classes

MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming

Class status: Established
Role: Student
. 5% complete

Submitted Assignments

MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 7, HW 1
#!/usr/bin/env python

def nestEggFixed(salary, save, growthRate, years):
    nestEgg = [salary * save * 0.01]
    for year in range(1, years):
        thisYear = nestEgg[year - 1] * (1 + 0.01 * growthRate) + salary * save * 0.01
        nestEgg.append(thisYear)
    return nestEgg

def nestEggVariable(salary, save, growthRates):
    nestEgg = []
    previous = 0
    for rate in growthRates:
        previous = previous * (1 + 0.01 * rate) + salary * save * 0.01
        nestEgg.append(previous)
    return nestEgg

def postRetirement(savings, growthRates, expenses):
    account = []
    previous = savings
    for rate in growthRates:
        previous = previous * (1 + 0.01 * rate) - expenses
        account.append(previous)
    return account

def findMaxExpenses(salary, save, preRetireGrowthRates, postRetireGrowthRates, epsilon):
    savings = nestEggVariable(salary, save, preRetireGrowthRates)[-1]
    low = 0
    high = savings
    guess = (low + high) / 2.0
    endBalance = postRetirement(savings, postRetireGrowthRates, guess)[-1]
    i = 1
    while abs(endBalance) > epsilon and i <= 100:
        if endBalance < 0:
            # expenses are too high
            high = guess
        else:
            # expenses are too low
            low = guess
        guess = (low + high) / 2.0
        endBalance = postRetirement(savings, postRetireGrowthRates, guess)[-1]
        i += 1
    return guess

Celauran 2 years ago