I need help with Problem 4 from PS4. I cannot figure out what my if statement should be in the binary search. If I do the problem manually, knowing the answer it's easy as I know when to assign the guess from low to high. I get the correct answers for parts 1-3, so I know that's not the issue.
def findMaxExpenses(salary, save, preRetireGrowthRates, postRetireGrowthRates, epsilon): low = 0 est_high = nestEggVariable(salary, save, preRetireGrowthRates) high = est_high[-1] # amount of money at start of retirement guess = (low + high)/2.0 retirement_list = postRetirement(high, postRetireGrowthRates, guess) money_end_retirement = retirement_list[-1] print money_end_retirement while abs(money_end_retirement) > epsilon: print guess # printing the expenses guess each iteration if ?????: high = guess guess = (low + high) / 2.0 retirement_list = postRetirement(high, postRetireGrowthRates, guess) money_end_retirement = retirement_list[-1] else: low = guess guess = (low + high) /2.0 retirement_list = postRetirement(high, postRetireGrowthRates, guess) money_end_retirement = retirement_list[-1] print "final estimate is ", guess Am I on the right track? Any help is greatly appreciated! :)
Yes, you're on the right track. Figure out how your guess affects the money_end_retirement, and you will know what to check for in your IF statement.
Thanks Aslander!! Turns out my original assumption of if money_end_retirement < 0: was correct, but I had the wrong value for my savings in postRetirement function. It should've been the money at the start of retirement. It threw me off!
Join our real-time social learning platform and learn together with your friends!