Week06 Review Recursive Functions

You might also like

Download as ppsx, pdf, or txt
Download as ppsx, pdf, or txt
You are on page 1of 18

Week 7

Course Review & Advanced Topics

Recursive Functions

Instructor: Asst. Prof. LIN Shang-Wei


Email: shang-wei.lin@ntu.edu.sg

Introduction to Computational Thinking 1


Topic Outline

How to Write Recursive Functions

How to Trace Recursive Functions

Introduction to Computational Thinking 2


How to Write Recursive Functions

Introduction to Computational Thinking 3


What Is A Recursive Function?

• A function that invokes itself


Recursive Function
• naturally supports divide-and-conquer

def recursiveFunc(param1, param2, …):


if exp: # base case (conquer)

return value
General Form:
else: # recursive step (divide)
recursiveFunc(subproblem1)
recursiveFunc(subproblem2)

return value

Introduction to Computational Thinking 4


How to Write a Recursive Function?

Assume you had


Determine the
finished the
interface (signature)
implementation of • Base Case (Conquer):
of the function
the function Solve the primitive case, and then
return the result
• What is the function name?
• What is the functionality of • Recursive Step (Divide)
the function? - Decompose the problem into
Develop the subproblems (with the same
• How many parameters?
function body structure)
What are they?
- Call the function to solve each
• What is the return object?
subproblem
- Compose the final result from
subproblems, and then return it

Introduction to Computational Thinking 5


Example
max
Develop a recursive function to find out the maximum element in a list
3 1 7 2 4

Step 1: Determine the name, functionality, and interface of the function

Functionality: To find to the maximum element in a list


def maxNum(aList):
Input: aList, the target list of integers

Output: The maximum integer in aList

Step 2: Assume you had finished the implementation of the function


So that we can use maxNum() to solve subproblems!!

Introduction to Computational Thinking 6


Example (cont.)

Step 3: Develop the function body of maxNum()

• Base Case (Conquer): • Recursive Step (Divide):

If aList has only one element If aList has more than one element

max

max

leftMax: 3 rightMax: 7
3
3 1 7 12 74 2 4

Introduction to Computational Thinking 7


Example (cont.)

max
def maxNum(aList):

if len(aList) == 1: # base case (conquer) 3

return aList[0]

max

else: # recursive step (divide)


leftMax = aList[0]
rightMax = maxNum(aList[1:]) leftMax: 3 rightMax: 7
return max(leftMax, rightMax) 3 1 7 2 4

Introduction to Computational Thinking 8


Q1: What is the output of the following Python program?

objectA = [3,1,2,4,7,5]
A.3
def funcA(aList):
if len(aList) == 1: ✔ B.1
return aList[0] C.2
D.4
else: E.7
elementA = aList[0] F.5
elementB = funcA(aList[1:])
min
if elementA <= elementB:
return elementA
else:
return elementB elementA: 3 elementB

print(funcA(objectA)) 3 1 7 2 4 5

Introduction to Computational Thinking 9


How to Trace A Recursive Function?

Introduction to Computational Thinking 10


How to Trace A Recursive Function?

Try to draw the CALL GRAPH!!!

2
input = 1 def funcB(par):
1
result = funcA(input) + 1 return par+3
6
1 5
def funcA(a):
2
return funcB(a+1)

Introduction to Computational Thinking 11


Example

def maxNum(aList): aList: [7, 5]


def maxNum(aList): aList: [2, 7, 5] if len(aList) == 1:
if len(aList) == 1: return aList[0]
return aList[0] else:
else: 7 leftMax = aList[0]
2 leftMax = aList[0] 5 rightMax = maxNum(aList[1:])
7 rightMax = maxNum(aList[1:])
return max(leftMax, rightMax)
7
return max(leftMax, rightMax)
7

def maxNum(aList): aList: [5]


if len(aList) == 1:
objectA = [2,7,5]
return aList[0]
print(maxNum(objectA))
5
else:
leftMax = aList[0]
rightMax = maxNum(aList[1:])
return max(leftMax, rightMax)

Introduction to Computational Thinking 12


Q2: What is the output of the following Python program?

num = 0
objectA = [2,7,5]

def maxNum(aList):
global num
num += 1 A.2
B.7
if len(aList) == 1:
return aList[0] C.5
else: ✔ D.3
leftMax = aList[0] E.9
rightMax = maxNum(aList[1:]) F.12
return max(leftMax, rightMax)
G.14
maxNum(objectA)
print(num)

Introduction to Computational Thinking 13


Q3: What is the output of the following Python program?

objectA = [2,7,5]

def funcA(aList):
if len(aList) == 1: A.2
return aList[0]
B.7
else: C.5
return aList[0] + funcA(aList[1:]) D.3
E.9
print(funcA(objectA)) F.12
✔ G.14

Introduction to Computational Thinking 14


Q4: What is the output of the following Python program?

A.* ✔ D. *
objectA = [1,2,3]
* **
def funcA(param1, param2): * ***
if len(param1) == 1:
print(param2 * param1[0]) B.** E.***
** **
else:
print(param2 * param1[0]) ** *
funcA(param1[1:], param2)
C.*** F.*
funcA(objectA, "*") *** ***
*** **

Introduction to Computational Thinking 15


Q4 Call Graph Explained
param1: [2, 3] , param2 = ‘*’
param1: [1, 2, 3], param2 = ‘*’ def funcA(param1, param2):
def funcA(param1, param2): if len(param1) == 1:
if len(param1) == 1: print(param2 * param1[0])
print(param2 * param1[0])
else:
else: print(param2 * param1[0]) **
print(param2 * param1[0]) * funcA(param1[1:], param2)
funcA(param1[1:], param2)

param1: [3] , param2 = ‘*’


def funcA(param1, param2):
if len(param1) == 1:
objectA = [1,2,3]
print(param2 * param1[0]) ***
funcA(objectA, ‘*’)
else:
* print(param2 * param1[0])
Output: ** funcA(param1[1:], param2)
***
Introduction to Computational Thinking 16
Q5: What is the output of the following Python program?

objectA = [1,2,3] A.* D. *


* **
def funcA(param1, param2): * ***
if len(param1) == 1:
print(param2 * param1[0]) E.***
B.** ✔
else: ** **
funcA(param1[1:], param2) ** *
print(param2 * param1[0])
C.*** F.*
funcA(objectA, "*") *** ***
*** **

Introduction to Computational Thinking 17


Q5 Call Graph Explained
param1: [2, 3] , param2 = ‘*’
param1: [1, 2, 3] , param2 = ‘*’ def funcA(param1, param2):
def funcA(param1, param2): if len(param1) == 1:
if len(param1) == 1: print(param2 * param1[0])
print(param2 * param1[0])
else:
else: funcA(param1[1:], param2)
funcA(param1[1:], param2)

print(param2 * param1[0]) **
print(param2 * param1[0]) *
param1: [3] , param2 = ‘*’
def funcA(param1, param2):
if len(param1) == 1:
objectA = [1,2,3]
print(param2 * param1[0]) ***
funcA(objectA, ‘*’)
else:
*** funcA(param1[1:], param2)
Output: ** print(param2 * param1[0])
*
Introduction to Computational Thinking 18

You might also like