care to comment on this code for problem 4?
def findMaxExpenses(salary,save,preRetireGrowthRates,postRetireGrowthRates,epsilon): preretirement=nestEggVariable(salary,save,preRetireGrowthRates) index=len(preRetireGrowthRates)-1 startmoney=preretirement[index] low=0 high=startmoney counter=1 guess=startmoney/2.0 while guess<startmoney and counter<100: expenses=guess post_Retirement=postRetirement(startmoney,postRetireGrowthRates,expenses) index2=len(postRetireGrowthRates)-1 endmoney=post_Retirement[index2] if endmoney<epsilon: high=guess else: low=guess guess=(low+high)/2.0 counter+=1 print expenses, endmoney,counter if endmoney<epsilon and endmoney>-epsilon: break def nestEggVariable(salary, save, growthRates): cashpile=[] money=0 counter=0 for counter in range (0,len(growthRates)): money=money+(0.01 * growthRates[counter]) * money+(salary *(.01 *save)) cashpile.append(money) return cashpile def postRetirement(savings, growthRates, expenses): cashpile=[] money=savings counter=0 for counter in range (0,len(growthRates)): money=money+(0.01 * growthRates[counter]) * money-expenses cashpile.append(money) return cashpile 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 # Output should have a value close to: # 1229.95548986
pls use pastebin or dpaste.com to post code. does it work?? were you looking for style or implementation comments?
It finds the answer in 20 iterations. I had a bit of a problem deciding how to make the program stop running. I made a condition that stopped the counter if the ending balance was between epsilon and -epsilon. My question is if epsilon wasn't .01 this wouldn't work as well, is there a better way to make an end condition?
Don;t think so.. You have to decide how precise you want your answer to be - the more precise, the more iterations. I imagine since the problem concerns money, you could round everything to cents, multiply by one-hundred and work in integers but that is sorta the same thing.
You really will need to use a code host, github, dpaste, something like that, to put up that much code. Then people can easily grab a copy, put in some print statements and play around with your script a bit.
Join our real-time social learning platform and learn together with your friends!