Wednesday, September 25, 2013
Solution to MIT OCW Assignment problem set#4 (Introduction to Computer Science and Programming Instructor(s) Prof. Eric Grimson Prof. John Guttag)
Check out the cool solutions, All work perfectly fine and have been verified upto the output:
Qn: Problem 4.
Write a function, called findMaxExpenses, which takes five arguments: a salary
(salary), a percentage of your salary to save (save), a list of annual growth percentages
on investments while you are still working (preRetireGrowthRates), a list of annual
growth percentages on investments while you are retired (postRetireGrowthRates), and
a value for epsilon (epsilon). As with problems 2 and 3, the lengths of
and postRetireGrowthRates determine the number of years you
plan to be working and retired, respectively.
Use the idea of binary search to find a value for the amount of expenses you can
withdraw each year from your retirement fund, such that at the end of your retirement,
the absolute value of the amount remaining in your retirement fund is less than epsilon
(note that you can overdraw by a small amount). Start with a range of possible values
for your annual expenses between 0 and your savings at the start of your retirement
(HINT #1: this can be determined by utilizing your solution to problem 2). Your function
should print out the current estimate for the amount of expenses on each iteration
through the binary search (HINT #2: your binary search should make use of your
solution to problem 3), and should return the estimate for the amount of expenses to
withdraw. (HINT #3: the answer should lie between zero and the initial value of the
savings + epsilon.) Complete the implementation of:
Write your code in the appropriate place in the template. To test your function,
run the test cases in the test function testFindMaxExpenses(). You should add
additional test cases to this function to further test your code.
Solution:
def findMaxExpenses(salary, save, preRetireGrowthRates, postRetireGrowthRates,
epsilon):
# TODO: Your code here.
retfund=salary * save * 0.01
for i in preRetireGrowthRates[1:] :
retfund = retfund*( 1+ .01 * i)
retfund = retfund + salary * save * 0.01
print retfund
# retfund=5266.76
cnt=0
low = 0
high= retfund
s = (low + high)/2.0
spendfund = retfund
while abs(spendfund) >= epsilon and cnt <= 100:
cnt=cnt +1
spendfund = retfund
spendfund = spendfund * (1 + 0.01 * postRetireGrowthRates[0]) - s
for j in postRetireGrowthRates[1:]:
spendfund = spendfund * (1 + 0.01 * j) - s
print ("haha", spendfund)
if spendfund > 0 :
low = s
else:
high = s
s = (low+high)/2.0
print("s values",s,"Ya",cnt )
def testFindMaxExpenses():
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 expenses
testFindMaxExpenses()
Ouput expected :
# Output should have a value close to:
# 1229.95548986
# TODO: Add more test cases here.
My output:
('s values', 1229.9529787158967, 'Ya', 20)
Link to the question:
http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00-introduction-to-computer-science-and-programming-fall-2008/assignments/pset4.pdf
Thanks for watching, keep watching for more solution to assignment problems.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment