


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
That's the code that can be made into a recursive function call. } } These are the same! Page 11 ...
Typology: Exercises
1 / 26
This page cannot be seen from the preview
Don't miss anything!



















Recursive Patterns Recursion I 2
What is Recursion? Gluing functions together... ...where the sub-problems involve the problem itself! A recursive function is a function that invokes itself. With recursion, the solution to a problem depends on solutions to smaller instances of the same problem Recursion I
Self-Containing Patterns 5 Fractals , found in nature and mathematics, are examples of patterns that contain smaller copies of themselves. This fern image is made of three smaller ferns. Recursion I
Review: functions calling other functions (in anticipation of writing functions that call themselves) def print4(s): print2(s) print2(s) def print2(s): print(s) print(s) print4( 'okay' ) def print4(s): print2(s) print2(s) print4( 'okay' ) def print2(s): print(s) print(s) def print2(s): print(s) print(s) def print4(s): print2(s) print2(s) print4( 'okay' ) Which would work? Why/why not? Recursion I
Our first recursive function: countDown Let’s write a function that prints the integers from n down to 1 ( without using loops ): In [ ]: countDown(5) 5 4 3 2 1 8
In [ ]: countDown(4) 4 3 2 1 print(4) countDown(3) Recursion I
print(5) countDown(4)
countDown: More Cases When you’re solving a recursive problem, you can start by writing many elif cases. Continue until you see a pattern develop.
Recursion I (^10)
} } These are the same!
countDown: More Cases You can simplify the pattern by using a recursive call to the same function with different parameters (which will send you into a different elif case).
Recursion I (^11) } Now it’s recursive
countDown: Recursive Case The recursive case. One we understand how to write a recursive else case, we can eliminate unnecessary elif cases. Sometimes we can leave out the base case too, if it doesn’t do anything.
Recursion I (^13)
Structure of Recursion All recursive functions must have two types of cases:
Invocation of countDown(3) Anatomy of function call frames
if n>0: print(n) countDown(n-1) countDown(3)
n 3
Recursion I (^16)
if True: print(n) countDown(n-1) countDown(3) n 3
if True: print( 3 ) countDown(n-1) countDown(3) n 3
if True: print( 3 ) countDown( 3 - 1) countDown(3) if True: n 3 print( 3 ) countDown( 2 ) countDown(3) n (^3) if n>0: print(n) countDown(n-1) countDown(2) If 2 >0: n 2 print(n) countDown(n-1) countDown(2) if True: n 2 print(n) countDown(n-1) countDown(2) n 2 if True: print( 3 ) countDown( 2 ) countDown(3) n (^3) if True: print( 2 ) countDown(n-1) countDown(2) n 2
if True: print( 2 ) countDown( 2 - 1) countDown(2) n 2 if n>0: print(n) countDown(n-1) countDown(1) if True: n 1 print( 2 ) countDown( 1 ) countDown(2) n 2 if^1 >0: print(n) countDown(n-1) countDown(1) if True: n 1 print(n) countDown(n-1) countDown(1) if True: n 1 print( 1 ) countDown(n-1) countDown(1) n 1
if True: print( 1 ) countDown( 0 ) countDown(1) if True: n 1 print( 1 ) countDown( 0 ) countDown(1) n 1 if False: print(n) countDown(n-1) countDown(0) n 0 if True: print( 2 ) countDown( 1 ) countDown(2) n 2 if False: print(n) countDown(n-1) countDown(0) n 0 Invocation of countDown(3) Draw the diagram of function call frames Recursion I (^17)
**- 1
"Maximum recursion depth exceeded" In practice, the infinite recursion examples will terminate when Python runs out of resources for creating function call frames, leading to a maximum recursion depth exceeded error message: Recursion I (^19)
What does this function do? def mystery(n): if n < 1: pass else: mystery(n - 1) print(n) What does mystery(3) print? Recursion I (^20)