MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 7, HW 1
# Problem Set 4
# Name: lf7
# Collaborators: Santa Claus, Rudolf and Sarah Palin
# Time: Some even finite sum of two prime numbers, in units of lightyears.
#
# Problem 1
#
def nestEggFixed(salary, save, growthRate, years):
"""
- salary: the amount of money you make each year.
- save: the percent of your salary to save in the investment account each
year (an integer between 0 and 100).
- growthRate: the annual percent increase in your investment account (an
integer between 0 and 100).
- years: the number of years to work.
- return: a list whose values are the size of your retirement account at
the end of each year.
"""
# TODO: Your code here.
n=0
F=[(salary)*save*.01];
while n+1<years:
F.append(F[n]*(1+.01*growthRate)+F[0]);
n=n+1;
return F
def testNestEggFixed():
salary = 10000
save = 10
growthRate = 15
years = 5
savingsRecord = nestEggFixed(salary, save, growthRate, years)
print savingsRecord
# Output should have values close to:
# [1000.0, 2150.0, 3472.5, 4993.375, 6742.3812499999995]
#testNestEggFixed()
#
# Problem 2
#
def nestEggVariable(salary, save, growthRates):
# TODO: Your code here.
"""
- salary: the amount of money you make each year.
- save: the percent of your salary to save in the investment account each
year (an integer between 0 and 100).
- growthRate: a list of the annual percent increases in your investment
account (integers between 0 and 100).
- return: a list of your retirement account value at the end of each year.
"""
n=0
F=[(salary)*(save)*.01];
while n+1<=len(growthRates)-1:
F.append(F[n]*(1+.01*growthRates[n])+F[0]);
n=n+1;
return F
def testNestEggVariable0():
salary = 10000
save = 10
growthRates = [3, 4, 5, 0, 3]
savingsRecord = nestEggVariable(salary, save, growthRates)
print('test 0')
print savingsRecord
# Output should have values close to:
# [1000.0, 2040.0, 3142.0, 4142.0, 5266.2600000000002]
# TODO: Add more test cases here.
def testNestEggVariable1():
salary= 20000
save=5
growthRates=[3,4,5,0,3]
savingsRecord= nestEggVariable(salary,save,growthRates)
print('test 1')
print savingsRecord
def testNestEggVariable2():
salary= 20000
save=10
growthRates=[3,4,5,0,3]
savingsRecord= nestEggVariable(salary,save,growthRates)
print('test 2')
print savingsRecord
def testNestEggVariable3():
salary= 30000
save=10
growthRates=[3,4,5,0,3]
savingsRecord= nestEggVariable(salary,save,growthRates)
print('test 3')
print savingsRecord
##testNestEggVariable0();
##testNestEggVariable1();
##testNestEggVariable2();
##testNestEggVariable3();
#
# Problem 3
#
def postRetirement(savings, growthRates, expenses):
"""
- savings: the initial amount of money in your savings account.
- growthRate: a list of the annual percent increases in your investment
account (an integer between 0 and 100).
- expenses: the amount of money you plan to spend each year during
retirement.
- return: a list of your retirement account value at the end of each year.
"""
# TODO: Your code here.
n=0
F=[savings*(1+.01*growthRates[n])-expenses];
for n in range(0,len(growthRates)-1):
F.append(F[n]*(1+.01*growthRates[n+1])-expenses);
n=n+1;
return F
def testPostRetirement0():
savings = 100000
growthRates = [10, 5, 0, 5, 1]
expenses = 30000
savingsRecord = postRetirement(savings, growthRates, expenses)
print '\n PR 0= ',savingsRecord
# Output should have values close to:
# [80000.000000000015, 54000.000000000015, 24000.000000000015,
# -4799.9999999999854, -34847.999999999985]
# TODO: Add more test cases here.
def testPostRetirement1():
savings = 200000
growthRates = [3, 5, 0, 5, 1,3,6,1,2,4,5,10]
expenses = 30000
savingsRecord = postRetirement(savings, growthRates, expenses)
print '\n PR 1= ',savingsRecord
def testPostRetirement2():
savings = 100000
growthRates = [10, 15, 10, 25, 11]
expenses = 30000
savingsRecord = postRetirement(savings, growthRates, expenses)
print '\n PR 2= ',savingsRecord
##testPostRetirement0()
##testPostRetirement1()
##testPostRetirement2()
#
# Problem 4
#
def findMaxExpenses(salary, save, preRetireGrowthRates, postRetireGrowthRates,
epsilon):
"""
- salary: the amount of money you make each year.
- save: the percent of your salary to save in the investment account each
year (an integer between 0 and 100).
- preRetireGrowthRates: a list of annual growth percentages on investments
while you are still working.
- postRetireGrowthRates: a list of annual growth percentages on investments
while you are retired.
- epsilon: an upper bound on the absolute value of the amount remaining in
the investment fund at the end of retirement.
"""
# TODO: Your code here.
#little check before the program is started;
assert epsilon > 0, 'Episolon must be greater than zero, not ' + str(epsilon)
savings = nestEggVariable(salary, save, preRetireGrowthRates)[-1]
low = 0
high = savings + epsilon
guess = low+(low - high)/2.0 #mid point
endRetirement = postRetirement(savings, postRetireGrowthRates, guess)[-1]
while abs(endRetirement) > epsilon:
if endRetirement > epsilon:
low = guess
else:
high = guess
guess = low+(high - low)/2.0
endRetirement = postRetirement(savings, postRetireGrowthRates, guess)[-1]
return guess
# Binary Search Example from the lecture for reference
'''def bsearch(growthRates, epsilon, growthRates[n], len(growthRates)-1, calls):
print growthRates,len(growthRates)-1 , calls
if (last - first) < 2: return s[first] == e or s[last] == e
mid = first + (last - first)/2
if s[mid] == e: return True
if s[mid] > e: return bsearch(s, e, first, mid - 1, calls+1)
return bsearch(s, e, mid + 1, last, calls + 1)
'''
def testFindMaxExpenses0():
salary = 10000
save = 10
preRetireGrowthRates = [3, 4, 5, 0, 3]
postRetireGrowthRates = [10, 5, 0, 5, 1]
epsilon = .01
expenses = findMaxExpenses(salary, save, preRetireGrowthRates,
postRetireGrowthRates, epsilon)
print 'findMax0= ',expenses
def testFindMaxExpenses1():
salary = 100000
save = 10
import random;
a=random.randrange(0,15,1)
preRetireGrowthRates = [a]
while len(preRetireGrowthRates)<20:
preRetireGrowthRates.append(a);
postRetireGrowthRates = [10, 5, 0, 5, 1,4,2,9,4]
epsilon = .01
expenses = findMaxExpenses(salary, save, preRetireGrowthRates,
postRetireGrowthRates, epsilon)
print 'findMax1= ',expenses
def testFindMaxExpenses2():
salary = 30000
save = 10
import random;
a=random.randrange(0,15,1)
preRetireGrowthRates = [a]
while len(preRetireGrowthRates)<20:
preRetireGrowthRates.append(a);
postRetireGrowthRates = [10, 5, 0, 5, 1]
epsilon = .01
expenses = findMaxExpenses(salary, save, preRetireGrowthRates,
postRetireGrowthRates, epsilon)
print 'findMax2= ',expenses
testFindMaxExpenses0()
testFindMaxExpenses1()
testFindMaxExpenses2()
# Output should have a value close to:
# 1229.95548986
# TODO: Add more test cases here.
lf7
1 year ago