Open study

is now brainly

With Brainly you can:

  • Get homework help from millions of students and moderators
  • Learn how to solve problems with step-by-step explanations
  • Share your knowledge and earn points by helping other students
  • Learn anywhere, anytime with the Brainly app!

A community for students.

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)
I got my questions answered at in under 10 minutes. Go to now for free help!
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.

Get this expert

answer on brainly


Get your free account and access expert answers to this and thousands of other questions

It isn't really. You could pass in the list (empty at first) and as each recursion returns, it appends/prepends to the list. Take a simple recursion like reversing a word, which is a string, which is a list of characters.
so for the factorial example, what would the code look like? def fact(n): if n == 0 return: [1] return [] + [fact(n-1] This seemed reasonable to me, but in fact (excuse the pun!) doesn't work!

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

Wow... those are some really neat ideas... not immediately intuitive to me, though... which is why recursive methods while elegant in execution, seem more complex to construct (IMHO)
they were't intuitive to me either, i had to work it out and it took several tries. i usually start by writing out in text what i am trying to accomplish and the steps/actions/resources it will take to accomplish it - then try to make that into code then debug it.
Yeah... me too... only when I think it through that way, the iterative method seems to jump out at me and I have to 'force' myself to try to think of it recursively... But many kudos for your efforts! I had a feeling you might be able to do it with "sub functions"...
well ... the functions i wrote are recursive procedures but they are iterative processes
here is one that is a recursive process - it builds up a chain of deferred operations i used the code from your second post :) it's pretty clean - not as messy as the others i posted
seems like the code is inching closer to the 'ideal' that I envision (if it even exists?!?)... but I'm not sure the output is correct... fa(5) gives me: [5, 20, 60, 120, 120] whereas I was hoping for [1,1,2,6,24,120]

Not the answer you are looking for?

Search for more explanations.

Ask your own question