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

Looking for something else?

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