#๐ Having trouble understanding how this recursion problem works
43 messages ยท Page 1 of 1 (latest)
@charred linden
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
!code
hi please send text
def helper(sum_so_far,index):
if index == len(s):
if sum_so_far == 100:
return 1
return 0
return helper(sum_so_far,index + 1) + helper(sum_so_far + s[index],index + 1)
return helper(0,0)```
sorry it took me so long
and what is your question about it?
i'm confused about how the recursion actually works, how does it go back up the recursion line? I understand that helper will go to helper(0,3) but afater that how does it go back up after doing helper(sum_so_far + s[2], 2+1)?
what do you mean by go back up?
I plugged this into online pythong tutor to see the steps of the code and saw that hel,per had an instance where it was helper(random,2), meaning that the index was decremented somehow?
what do you mean decremented?
index starts at 0 and goes up by 1 each time
are these the correct steps? helper(0,0) -> helper(0,1) -> helper(0,2) -> helper(0,3) -> returns 0 -> helper(0 + s[2], 3) -> helper(sum_so_far + s[1],2) -> helper(sum_so_far + s[0],1)?
ok lets have a look
[25, 50, 75, 100, 125, 150]
with this data
helper(0,0) is called
len(s) == 6
so the condition is False
then it calls helper(0, 1) + helper(25, 1)
that's where I'm confused, I thought the first recursive call needs to be run completely before moving on to the other recursive call?
you add each of them step by step
oh ok so it would be 0+0 for the first step?
because sum_so_far for both doesn't equal 100?
helper(0, 0)
helper(0, 1) + helper(25, 1)
helper(0, 2) + helper(50, 2) and helper(25, 2) + helper(75, 2)
this part confuses me, helper(25, 2) + helper(75, 2)
how come the two isn't equal to 3?
it's called with 25 right?
yeah
and the second index is 50
so sum_so_far25 + s[1]50
yes
I'm confused about what happens when sum_so_far is equal to 100, I somewhat understand that the value 1 is returned but not sure what happens after this?
since it returns, that line of recursion is done
there are a lot of recursions happening at the same time
what happens if sum_so_far exceed 100?
it returns 0
when all recursions are done, each of them returns either a 0 or a 1
they get added 1 + 0 + 1...
With the given data what happens first, 100 returning 1 or 25 + 75 returning 1?
they both happen
i'm not sure of the order
oh ok I think I'm starting to understand it more and more, I'm going to continue playing around with the values I use for s, I appreicate your help!
printing stuff will help
tho this is a big recursion and a bit hard to unravel
Thank you I will try this problem with a smaller list of values, s, and print out the results
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.