anonymous
  • 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.
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • 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()
anonymous
  • anonymous
This code is also at pastebin http://pastebin.com/rfH7y6TW Sorry about the hard returns. The Pastebin copy does not have them.
anonymous
  • 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

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • 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
anonymous
  • 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

Looking for something else?

Not the answer you are looking for? Search for more explanations.