Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 24 Online
OpenStudy (anonymous):

I have working functions for Problem set 4 but I would like to see if there is a way to make them more compact. I feel like I am missing the simple tricks. The code below is for Prob. 4 but contains the code for Prob. 2 and 3 as well.

OpenStudy (anonymous):

Here is my code ##Find max expenses allowed for a zero balance in savings at the endo fo term import math def nestEggVariable(salary, save, growthRates): assert salary > 0 and save > 0 account = [] growRate = [] cnt = 1 for i in growthRates : growRate.append (float (i)/100.0 + 1.0) saveRate = float (save)/100 amount = salary * save/100 years = len (growthRates) account. append (float (amount)) while cnt < years : amount = ((account [cnt-1]) * (growRate [cnt] ) + (salary * saveRate)) account.append (amount) cnt += 1 ## print cnt,amount return account def postRetirement(savings, growthRates, expenses): assert savings > 0 and expenses > 0 account = [] growRate = [] cnt = 0 for i in growthRates : growRate.append (float (i)/100.0 + 1.0) years = len (growthRates) amount = (float (savings)) spend = float (expenses) while cnt < years : amount = (amount * (growRate [cnt] )) - (spend) account.append (amount) cnt += 1 ## print cnt,amount return account def findMaxExpenses(salary, save, preRetireGrowthRates, postRetireGrowthRates, epsilon): prevalues= nestEggVariable(salary, save, preRetireGrowthRates) savings = prevalues [-1] ## Find final savings amount difference = 100.0 ## Set the initial value to coapare to epsilon years = float((len( postRetireGrowthRates))) expenses = savings/years ## seet initial expenses guess to evenly spread avings over time while abs (difference) > epsilon: # test for epsilon account = postRetirement(savings, postRetireGrowthRates, expenses) difference = account [-1] ## get final account value expenses = expenses + (difference/years) ## rest expenses by adding average error, if value is negative then expenses decrease for next run## print difference, return expenses 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 '\n'' Expenses', expenses # Output should have a value close to: # 1229.95548986 # TODO: Add more test cases here. testFindMaxExpenses()

OpenStudy (anonymous):

This code is also at pastebin http://pastebin.com/rfH7y6TW Sorry about the hard returns. The Pastebin copy does not have them.

OpenStudy (anonymous):

I have my code here : About the function nestEggVariable 1) First simple thing is that you can omit 'float' for readability, because if one number is already in float as 100.0 or 1.0 then the othe one will be converted as well. But that doesn't really make it compact but 2) why the code calculates saveRate = float (save)/100 amount = salary * save/100

OpenStudy (anonymous):

.....and then in the loop again salary * saveRate if you have the value already in amount[0] 3) you don't need to have 'cnt' variable either if you use 'for' instead of 'while' loop But otherwise in my mind thats it. Btw clever approximation expenses = expenses + (difference/years) in findMaxExpenses if I compare with the one thaught in lectures it converges really quickly 20+ iterations vs ~5 iterations

OpenStudy (anonymous):

Good suggestions all. I will remember to use type coercion on my floats. I am not sure I understand how I would substitute a for loop. What variable would I iterate and how would I capture an index value of the variable to use inside the loop? Thanks for the help

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!