A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
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.
anonymous
 5 years ago
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.

This Question is Closed

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Here 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 [cnt1]) * (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
 5 years ago
Best ResponseYou've already chosen the best response.0This code is also at pastebin http://pastebin.com/rfH7y6TW Sorry about the hard returns. The Pastebin copy does not have them.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0I 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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0.....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
 5 years ago
Best ResponseYou've already chosen the best response.0Good 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
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.