Intro Python Part3
Intro Python Part3
1
Function calls
What happens when a function calls another functions ?
When a function terminates, its call A new function call opens a new
frame is removed from the stack frame to store local variables
h() s “h “
g() s “g “
f start
g start
f() s “f “
h start
h done
g done
Call Stack
f done
(LIFO) 2
Calculating Factorial
• n! = 1*2*3*…(n-1)*n
• An iterative implementation:
Recursively:
n! = n*(n-1)! 4! = 4*3!
0! = 1 = 4*(3*2!)
= 4*(3*(2*1!))
=4*(3*(2*(1*0!)))
=4*(3*(2*(1*1)))
=4*(3*(2*1))
= 4*(3*2) = 4*6 = 24
4
Recursive Definition
Factorial
n! = n * (n-1)!
0! = 1
Smaller instance
Base condition
5
Recursive Implementation of
Factorial in Python
if n == 0:
return 1
return n * factorial(n-1)
6
Recursion
Recursive function:
A function whose implementation calls itself (with different
arguments).
Recursive Solution
A solution to a “large” problem using solutions to “small”
problems that assemble it.
7
Recursive factorial – step by step
factorial(
4)n
4
Returns…
8
Recursive factorial – step by step
factorial(
4)n
4
Returns…
9
Recursive factorial – step by step
factorial(
4)n
4
Returns…
4*…
10
Recursive factorial – step by step
factorial(
factorial(
4)n
3)n
4
3
Returns…
Returns…
4*…
11
Recursive factorial – step by step
factorial(
factorial(
4)n
3)n
4
3
Returns…
Returns…
4*…
12
Recursive factorial – step by step
factorial(
factorial(
4)n
3)n
4
3
Returns…
Returns…
4*…
3*…
13
Recursive factorial – step by step
factorial(
factorial(
4)n factorial(
3)n
2)n
4
3
2
Returns…
Returns…
4*… Returns…
3*…
14
Recursive factorial – step by step
factorial(
factorial(
4)n factorial(
3)n
2)n
4
3
2
Returns…
Returns…
4*… Returns…
3*…
15
Recursive factorial – step by step
factorial(
factorial(
4)n factorial(
3)n
2)n
4
3
2
Returns…
Returns…
4*… Returns…
3*…
2*…
16
Recursive factorial – step by step
factorial(
factorial(
4)n factorial(
3)n factorial(
2)n
4 1)n
3
2
Returns… 1
Returns…
4*… Returns…
3*… Returns…
2*…
17
Recursive factorial – step by step
factorial(
factorial(
4)n factorial(
3)n factorial(
2)n
4 1)n
3
2
Returns… 1
Returns…
4*… Returns…
3*… Returns…
2*…
18
Recursive factorial – step by step
factorial(
factorial(
4)n factorial(
3)n factorial(
2)n
4 1)n
3
2
Returns… 1
Returns…
4*… Returns…
3*… Returns…
2*…
1*…
19
Recursive factorial – step by step
factorial(
factorial(
4)n factorial(
3)n factorial(
2)n
4 1)n
3
2
Returns… 1
Returns…
4*… Returns…
3*… Returns…
2*…
1*…
20
Recursive factorial – step by step
factorial(
factorial(
4)n factorial(
3)n factorial(
2)n factorial(
4 1)n
3 0)n
2
Returns… 1
Returns… 0
4*… Returns…
3*… Returns…
2*… Returns…
1*…
21
Recursive factorial – step by step
factorial(
factorial(
4)n factorial(
3)n factorial(
2)n factorial(
4 1)n
3 0)n
2
Returns… 1
Returns… 0
4*… Returns…
3*… Returns…
2*… Returns…
1*…
1
22
Recursive factorial – step by step
factorial(
factorial(
4)n factorial(
3)n factorial(
2)n
4 1)n
3
2
Returns… 1
Returns…
4*… Returns…
3*… Returns…
2*…
1*1
23
Recursive factorial – step by step
factorial(
factorial(
4)n factorial(
3)n
2)n
4
3
2
Returns…
Returns…
4*… Returns…
3*…
2*1
24
Recursive factorial – step by step
factorial(
factorial(
4)n
3)n
4
3
Returns…
Returns…
4*…
3*2
25
Recursive factorial – step by step
factorial(
4)n
4
Returns…
4*6
26
Recursion
In every recursive call the problem is reduced.
27
General Form of Recursive Algorithms
28
Short Summary
Design a recursive algorithm by
1. Breaking of big problems to smaller problems
2. Solving base cases directly.
29
Example: Fibonacci Series
• Fibonacci series
0, 1, 1, 2, 3, 5, 8, 13, 21, 34
• Definition:
• fib(0) = 0
• fib(1) = 1
• fib(n) = fib(n-1) + fib(n-2)
30
Fibonacci
“Naturally” recursive
31
Example: Modulo
1. Base case?
2. What is the recursive call?
3. How do we use the result of the recursive call to
calculate the value for n?
32
Solution: Recursive Modulo
33
Recursive List sum
• Given a list of numbers, calculate their sum using a recursive
function.
13
13
34
Recursive List sum
13
13
13
35
Additional examples
• Calculating the sum of sub-lists
• Using slicing
• Using indices (more efficient)
• Odd-Even
36
Recursive Sub-lists Sum
• Input: a list of lists, each internal list contains only
numbers
• Output: a list that contains the sums of each internal
list from the input.
• For example:
• For [ [1,2,3], [4] ] the result is [6, 4]
37
Recursive Sub-lists Sum
• More examples:
In this call, the input contains only one list.
The list has no elements, so its sum is 0.
Easy!
Advancing towards the base condition
• The input is a list
• The base condition is an empty list
• A recursive call with a shorter list is considered to
be an easier case.
• Usually take off the first or last element
• In addition, we advance toward the base list: if we
take elements off the list, eventually we’ll get an
empty list.
Using the recursive call
Having an actual example may help
let’s consider this example [[1,2,3], [5],[6,1]]
input result
Our problem [[1,2,3], [5],[6,1]] [6, 5, 7]
Recursive call [[1,2,3], [5]] [6, 5]
• Our plan:
• Given a list:
• Put aside the last element
• Recursive call with a shorter list and save the
result
• Add to this result the sum of the last element we
put aside.
• Return the result.
And now in Python
Shorted version
Execution example for [[1,2,3], [4,5]]
62
Is the solution for Recursive Sum the
?most efficient
70
Odd-Even
71
Odd-Even
72
Odd-Even
73
Odd-Even
74