How do you append results to a list using recursion?
For example, using a basic factorial def:
def fact(n):
if n == 0: return 1
return n * fact(n-1)
This will return the "end" result, but what if I wanted to return a list of all the factorials... so that fact(5) would return [1, 1, 2, 6, 24, 120]?
This is a specific question about a general problem of variables or lists getting "reinitialized" every time during a recursive call!
For example: (SPOILER ALERT):
In 2008 PS4, this code will return the value of the *last* year:
def nestEggFixed(salary, save, growthRate, years):
if years == 1: return salary*(save/100.0) #base case, return annual contrib.
return salary*(save/100.0) + (1+growthRate/100.0)*nestEggFixed(salary,save,growthRate,(years-1))
# ^ each year adds annual contribution + interest*accumulated total
But the assignment wants a [list] for values after each year. This was easily done iteratively by appending result to a list during each loop, but recursively seems a lot harder...

Hey! We 've verified this expert answer for you, click below to unlock the details :)

I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!

http://dpaste.com/917357/

Looking for something else?

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

## More answers

Looking for something else?

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