Trying to understand recursion in Python - python

I've been trying to understand the following code, it's a recursion example from my Python book:
def mysum(L):
if not L:
return 0
else:
return L[0] + mysum(L[1:])
print(mysum([1, 2, 3, 4, 5]))
output: 15
I have a very hard time to understand how this works, and how it is returning 15.
I've tried to rewrite the code as:
def mysum(L):
if not L:
return 0
else:
temp = L[0] + mysum(L[1:])
print(temp)
return temp
mysum([1, 2, 3, 4, 5])
this outputs:
5
9
12
14
15
But i'm still not sure how this works, it's like it starts to sum backwards.
5 + 4 + 3 + 2 + 1
return L[0] + mysum(L[1:])
I know that functions on the right get executed before the function returns anything. In this case it's recursive, it calls itself until L has no elements in it. But if it calls itself again, wouldn't that mean that it again doesn't return anything? This is very confusing to me.

L[0] is the head of the list and L[1:] is the rest. In each call the function adds the first element and the sum of the remaining list.
So what is happening is:
mysum([1, 2, 3, 4, 5]) => 1 + mysum([2, 3, 4, 5])
mysum([1, 2, 3, 4, 5]) => 2 + mysum([3, 4, 5])
mysum([1, 2, 3, 4, 5]) => 3 + mysum([4, 5])
mysum([1, 2, 3, 4, 5]) => 4 + mysum([5])
mysum([1, 2, 3, 4, 5]) => 5 + mysum([])
mysum([]) => 0
After the last call everything everything returns.
Maybe it would be helpful for you to print not only your temp but also L.

it's like it starts to sum backwards.
Well that's because it kind of does, this is an example of tail recursion which is not optimized in python, imagine replacing the mysum(L[1:]) with the result in brackets, you would get something like this:
#L[0] + mysum(L[1:])
mysum([1,2,3,4,5])
1 + mysum([2,3,4,5])
1 + (2 + mysum([3,4,5]))
1 + (2 + (3 + mysum([4,5])))
1 + (2 + (3 + (4 + mysum([5]))))
1 + (2 + (3 + (4 + (5 + mysum([])))))
1 + (2 + (3 + (4 + (5 + 0))))
The inner most level of recursion must finish evaluating before the above levels can, so it only actually starts adding the numbers together once the list has been exhausted and then starts will last recursive call. (the end of the list)
But if it calls itself again, wouldn't that mean that it again doesn't return anything?
Well yes, but only until it can return something without requiring another recursive call, then it can return something, and then the level above can return, then the level above...

Let's follow your first part of code, we call it with the list [1,2,3,4,5]
The first call has L = [1,2,3,4,5] so gets into the second portion and does:
return 1 + mysum([2,3,4,5])
mysum is called again, now with a smaller list, which does:
return 2 + mysum([3,4,5])
Next up:
return 3 + mysum([4,5])
Then:
return 4 + mysum([5])
One more time with the normal flow:
return 5 + mysum([])
This time our passed list is empty and our function returns 0. This cascades back on the stack, which means mysum([5]) now evaluates to 5 (5+0=5), this leads to an evaluation of mysum([4,5]) etcetera all the way up to our first call which returns 15.

Consider this code:
def mysum0(L):
return 0
def mysum1(L):
return L[0] + mysum0(L[1:])
def mysum2(L):
return L[0] + mysum1(L[1:])
def mysum3(L):
return L[0] + mysum2(L[1:])
def mysum4(L):
return L[0] + mysum3(L[1:])
print(mysum4([1, 2, 3, 4]))
Each function mysum[n] sums a list of length n and delegates to mysum[n-1] to help it out. Can you understand how that works? The recursive mysum function is like all the mysum[n] functions combined into one. It only needs to know how to handle a length of list 0 and how to take care of one layer.

You can test the hypothetical question you raised: What happens when the recursion gets to the point that the list has only one element in it?
>>>print(mysum([5]))
returns:
5
Because it executes:
return L[0] + mysum(L[1:])
L[0] returns 5, and mysum(L[1:]) (because L[1:] doesn't exist with a list of length 1) returns 0.
So, now that the function has done that, it can compute the next mysum in the recursion, for L = [4, 5], which turns into:
return L[0] + mysum[L[1:]
which is equivalent to:
return 4 + mysum[5]
which, since we calculated mysum[5] = 5, is equivalent to:
return 4 + 5

Related

Missing number in a list

I need a little help, a have a list of integers - the difference between the consecutive elements is constant, but the list is unsorted and one element is missing. The function takes a list as an input, the output is an integer (the missing one).
I wrote this code:
def missing_number(lst):
lst.sort()
diff = lst[1] - lst[0]
for i in range(len(lst)-1):
if lst[i+1] - lst[i] != diff:
return int(lst[i] + diff)
It's working for this case: assert missing_number([1, 4, 2, 5]) == 3, but I can't make it work for a case like this: assert missing_number([2, 6, 8]) == 4 - the difference is more than 1 digit - it's returning 10.
A linear time solution: Compute the sum you'd expect if the number weren't missing, and subtract the actual sum:
def missing_number(lst):
expect = (min(lst) + max(lst)) * (len(lst) + 1) // 2
return expect - sum(lst)
The expected full sum is the average (which is (min+max)/2) multiplied by the number of numbers (which is len+1). I postpone the division until the end since min+max might be odd.
Yours fails the example [2, 6, 8] because you compute diff = 4, but it should be 2. You can't just always use lst[1] - lst[0]. You could for example use (lst[-1] - lst[0]) // len(lst) instead.
It doesn't like your code is not working as expected for difference is more than 1 digit.
It is purely because of sequence.
If you try the differ sequence with more with your code, it gonna definitely work. Eg:assert missing_number([2, 4, 8]) == 6
So you can not take difference between 1st 2 numbers. You have to take the minimum difference, so you can check by comparing difference with next 2 numbers & take the smallest.
You can try:
def missing_number(lst):
lst.sort()
final_diff = 0
diff0 = lst[1] - lst[0]
diff1 = lst[2] - lst[1]
if diff0 < diff1:
final_diff = diff0
else:
final_diff = diff1
for i in range(len(lst)-1):
if lst[i+1] - lst[i] != final_diff:
return int(lst[i] + final_diff)
print(missing_number([2, 6, 8]))
output:
4

How to use recursion to sum up a list of numbers up until a certain index

I have to write a code that makes use of recursion to sum numbers in a list up until the index is equal to a pre-determined integer value. i.e.
list = [1,4,8,9]
int = 2
sum = 1 + 4 (index 0 and 1)
below is my code so far, but I am struggling with the logic with my first if statement and hence it isn't working. I get the error 'int' object has no attribute 'index' Any help would be greatly appreciated (PS very new to coding - so sorry if my code isn't the best)!
# Sum Recursion
def Arecursion(Alist,index):
if index > 0: # if the index point in the list matches the integer return the sum
return Alist[index] + Arecursion(Alist,index-1)
else:
return 0
list_test = [1,4,6,7,10]
int_test = 2
print(Arecursion(list_test,int_test))
You are making it more complex that you need to. You just need a base case — the current index is too big, and the recursion — the value at the current index plus the rest:
def sum_rec(l,max_index, i=0):
if i >= max_index or i >= len(l): # base case
return 0
return l[i] + sum_rec(l, max_index, i+1) # recursion
sum_rec([1, 2, 3, 4], 0)
# 0
sum_rec([1, 2, 3, 4], 1)
# 1
sum_rec([1, 2, 3, 4], 2)
# 3
sum_rec([1, 2, 3, 4], 3)
#6

Understand where total value is stored in recursive function - Python

I am trying to understand where the total value is stored for example the return of this function is the int 15. Each number from the input list is taken and added one at a time and removed from the list but I cannot see where the temp value is stored I only get the complete total?
Also regarding return L[0] + mysum(L[1:])ifmysum(L[1:]) stores the list how can it be added with L[0] and if mysum(L[1:]) does not store the list where is the list stored is it not lost and then surely the program would not know what numbers to add next?
Code
def mysum(L):
print(L) # Trace recursive levels
if not L: # L shorter at each level
return 0
else:
return L[0] + mysum(L[1:])
total = mysum([1, 2, 3, 4, 5])
print("Total = ", total)
Return
[1, 2, 3, 4, 5]
[2, 3, 4, 5]
[3, 4, 5]
[4, 5]
[5]
[]
Total = 15
The intermediate running totals are never "stored" in a variable - they are passed down the call stack each time one of the recursive calls returns:
The first recursive call to return is mysum([]) which returns the number 0.
After this, the recursive call mysum([5]) returns 5 + 0 = 5.
After this the recursive call mysum([4,5]) returns 4 + 5 = 9.
Then the recursive call mysum([3,4,5]) returns 3 + 9 = 12.
Then mysum([2,3,4,5]) returns 2 + 12 = 14.
Then finally, the original non-recursive call mysum([1,2,3,4,5]) returns 1 + 14 = 15.
I have an interactive demo which shows how some recursive functions are computed step-by-step using a call stack. It may help you to understand how recursive functions are executed.
The value is "stored" using the fact that before returning an expression, you have to calculate both sides.
In your example, on the first call the last "return" will be "return 1 + mysum([2, 3, 4, 5])", then mysum will be evaluated, it will be evaluated to "return 2 + mysum([3, 4, 5])".
Finally, when you've reached the end of the list, mysum return 0, so it will be "return 1 + 2 + 3 + 4 + 5 + 0".

Python -While loop to recursive [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
How do you convert the code below to recursive ?
def fibonacci(n):
a, b = 0, 1
fibonacci = [0]
while a < n:
fibonacci.append(b)
a, b = b, a+b
print ('The fibonacci sequence is : '+" ".join(map(str,fibonacci)))
so basically I'm trying write a function fibonacci that receives a
number as parameter and computes the fibonacci series up to that number.
I was able to come up with the above iteration method but it has to be recursive.
This is what I have done so far in terms of converting to recursive but it's not giving me the output I need
def fibo(n, a = 0, b = 1, fib = [0]):
if a < n:
fib.append(b)
a, b = b, a + b
return fib
return fibo(n, a, b, fib)
how about this ?:
def fibonacci(n, a=0, b=1):
if a >= n : return [a]
return [a] + fibonacci(n,b,a+b)
[EDIT] Here's how it works:
The function progressively builds an array by adding one element [a] to the result of the next call to itself.
The first line allows it to stop when the target is reached. Without it, the second line of the function would keep calling itself and there would never be a result coming back from the recursion.
Because the parameters of a function are local to each call, the value of a and b in the second call are different from the previous ones.
If we follow the logic for fibonacci(7), we get:
1) fibonacci(n=7, a=0, b=1) ==> will return [0] + fibonacci(7,1,1).
2) fibonacci(n=7, a=1, b=1) ==> will return [1] + fibonacci(7,1,2).
3) fibonacci(n=7, a=1, b=2) ==> will return [1] + fibonacci(7,2,3).
4) fibonacci(n=7, a=2, b=3) ==> will return [2] + fibonacci(7,3,5).
5) fibonacci(n=7, a=3, b=5) ==> will return [3] + fibonacci(7,5,8).
6) fibonacci(n=7, a=5, b=8) ==> will return [5] + fibonacci(7,8,13).
7) fibonacci(n=7, a=8, b=13) ==> 8 >= 7 so the first line returns [8]
At that point there are no more recursive calls (the first line returns without calling the function again) and the return values start coming back up.
7) returns [8]
6) returns [5,8]
5) returns [3,5,8]
4) returns [2,3,5,8]
3) returns [1,2,3,5,8]
2) returns [1,1,2,3,5,8]
1) returns [0,1,1,2,3,5,8]
One way to think about recursive functions is to look only at the incremental work to be done on the result that would be produced by a prior parameter value. Most of the time this part of the logic applies backwards (i.e computing the end result based on a previous one). For example, a factorial can be thought of as the multiplication of a number with the factorial of the previous number.
This gives you the equivalent of the second line.
Once you have that down, all you need to decide is the condition that makes the recursion stop. Usually this corresponds to the smallest/simplest use case. For example, a factorial does not need to recurse when the number is less than 2 so the function can return 1 directly.
This gives you the equivalent of the first line.
As you can see in the above tracing, the function will proceed "forward" but actually ends up waiting for a result from itself (with different parameters) before being able to complete the process. This is how recursive functions work. The final result is typically built when the return values come back up from the stack of multiple self-calls.
Your fibonacci function is a bit trickier than a factorial because the series can only be computed from the original (0,1) values. Unlike the factorial, we don't have enough information to figure out the value of a and b based on the supplied parameter (n).
And, if you'd like to sink you teeth in cryptic code, here's a one line version:
def fibo(n,a=0,b=1):return [a]+fibo(n,b,a+b) if a < n else [a]
The point of a recursive implementation is to organize things like this:
if we're at a base case:
return the result for that base case
else:
call ourselves with a reduced case
possibly modify the result
return the result
For the base case, a < n, what you do should be related to what you do after the while loop in the iterative version. Adding the last value to the accumulator list fib and returning it makes sense. It may or may not be right, but it's at least in the right direction.
But in your recursive case, you're not calling yourself with a reduced case, you're just calling yourself with the exact same arguments. That's obviously going to be an infinite loop. (Well, Python doesn't do tail call elimination, so it's going to be a stack overflow, which shows up as a max recursion exception, but that's no better.)
So, what should you be doing? Something related to what happens inside the original non-recursive while loop. What you were doing there is:
fibonacci.append(b)
a, b = b, a+b
So, the equivalent is:
fib.append(b)
return fibo(n, b, a+b, fib)
Again, that may not be right, but it's in the right direction. So, if you get the idea, you should be able to carry on from there to debugging the full function.
I think that Dash's answer is correct, modulo a couple of colons. The original question ask for the computation, not printing.
I've added a dict to store computed values to speed things up a bit, and this code works for me:
fib = {}
fib[0] = 0
fib[1] = 1
def fibonacci(n):
if n not in fib.keys():
fib[n] = fibonacci(n - 1) + fibonacci(n - 2)
return fib[n]
if __name__=="__main__":
for i in range(10):
print i, fibonacci(i)
Output:
0 0
1 1
2 1
3 2
4 3
5 5
6 8
7 13
8 21
9 34
recursion is a functional heritage
Recursion is a concept that comes from functional style. Mixing imperative-style mutations (like append) and reassignments like a, b = b, a + b is a source of much pain and confusion for new programmers
def fibseq (n, a = 0, b = 1, seq = []):
if n == 0:
return seq + [a]
else:
return fibseq (n - 1, b, a + b, seq + [a])
for x in range (10):
print (fibseq (x))
# [0]
# [0, 1]
# [0, 1, 1]
# [0, 1, 1, 2]
# [0, 1, 1, 2, 3]
# [0, 1, 1, 2, 3, 5]
# [0, 1, 1, 2, 3, 5, 8]
# [0, 1, 1, 2, 3, 5, 8, 13]
# [0, 1, 1, 2, 3, 5, 8, 13, 21]
# [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
def fibonacci(n):
if n == 0:
return 0
elif n == 1
return 1
else
return fibonacci(n - 1) + fibonacci(n - 2)

Count elements in list via a recursive method

I'm trying to get a grasp on recursion. In general I understand what it does, but I'm having some trouble understanding how the following method works.
def count_numbers_in_list(list):
if list == []:
return 0
return 1 + count_numbers_in_list(list[1:])
Now I get that list[1:] returns everything from the given list except the first.
But what I can't get my head around is that we are never calling something like - 1 on the list itself. So this function works perfectly, but it would also make sense to me that this function returns a infinite loop.
I'm hoping that somebody could explain precisely to me what's happening here.
Thanks!
Now I get that list[1:] returns everything from the given list except the first.
Actually you've already found out the answer explaining why that function works perfectly.
return 1 + count_numbers_in_list(list[1:])
Every time recursion is performed, count_numbers_in_list is given a shorter list until there is no element left in it:
Let's break this down. Consider you have the below list:
lst = [1, 2, 3, 4]
count_numbers_in_list is given [1, 2, 3, 4] at first:
0 + count_numbers_in_list([1, 2, 3, 4])
(0 + (1 + count_numbers_in_list([2, 3, 4]))
(1 + (1 + count_numbers_in_list([3, 4]))
(2 + (1 + count_numbers_in_list([4]))
(3 + (1 + count_numbers_in_list([])))
(4 + (count_numbers_in_list([])))
(4 + 0)
Result is 4.
This recursive counting method is basically saying "if my list is empty, that's my base case. There are 0 elements here. Otherwise, I can count just one of the elements, and then I can count the rest of the list."
So "1+" is saying "count one of the elements". "count_numbers_in_list(list[1:])" is saying "count everything except the first element in the list."
Then you just think of it as deferred execution of some sort, or a stack if you're familiar with that. Say I have the list [1, 2, 3]. The recursive statement says "return 1 plus whatever you calculate as the result of count([2, 3])". Similarly, the answer to [2,3] is 1 plus whatever the answer to [3] is. 3 is 1 + the answer to [], and our base case says the answer to [] is 0. So [3] resolves as 1 + 0 = 1, and passes it back up to [2, 3]. That resolves as 1 + 1 = 2, and 2 gets passed back up to the result for [1, 2, 3]. That resolves as 1 + 2 = 3, and that's your final answer.

Categories

Resources