Professional Documents
Culture Documents
Week06 Review Recursive Functions
Week06 Review Recursive Functions
Week06 Review Recursive Functions
Recursive Functions
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
max
def maxNum(aList):
return aList[0]
max
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
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)
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)
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
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, "*") *** ***
*** **
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