Professional Documents
Culture Documents
Week # 9 Lecture # 17 & 18 Python Programming Iteration: Learning Objectives
Week # 9 Lecture # 17 & 18 Python Programming Iteration: Learning Objectives
Week # 9 Lecture # 17 & 18 Python Programming Iteration: Learning Objectives
https://youtu.be/fibHOcU-Tcw
https://youtu.be/uAVgwPQCRZw
3 Dr Umair Abdullah Recursive Functions for Nested Lists https://youtu.be/Y7tKlcfUE00
https://youtu.be/dNc-fleLgac
Learning Objectives
1. Solution of assignment # 5
2. Recursive functions using second method
a. Count , sum, member, delete, reverse, factorial, Fibonacci
3. Recursive functions for nested lists
PRE-REQUISITS
1. PyCharm is installed
2. You have learned basics of Python programming
3. You have executed iterative and recursive examples of last week lectures
SOLUTION (Assignment # 5)
QUESTION: Define two functions (one iterative and other recursive) in Python named
sub-it and sub-rec. Each of the functions will take three arguments; old-value, new-
value, and a list. They should replace old-value with the new-value from the given list.
Sample call;
SOLUTION:
A) ITERATIVE FUNCTION
B) RECURSIVE FUNCTION
Fibonacci Series example in last lecture already uses this method i.e. passing data to
recursvie calls with the help of additional variables. Now we shall use this method for
accumulating the result in the additional answer variable. We shall be redoing all the
example of last lecture using this new method.
RECURSIVE FUNCTIONS
with additonal ans variables
1. count recursive
print(cr2([10,20,30,40]))
# [10, 20, 30, 40] 0
# [20, 30, 40] 1
# [30, 40] 2
# [40] 3
# [] 4
4
2. Sum recursive
3. Member-recursive
def memr(a, x):
if x == []:
return False
elif a == x[0]:
return True
else:
return memr(a, x[1:])
Note that for the member function we don’t need to use the second method as we are not maintaing
any answer in this fuction. True is returned immediately when the element is found in the list (no
need to store it)
4. Delete-recursive
def dr2(a, x, ans=[]):
if x == []:
return ans
elif a == x[0]:
return dr2(a, x[1:], ans)
elif type(x[0]) is list:
ans1 = dr2(a, x[0], [])
ans.append(ans1)
return dr2(a, x[1:], ans)
else:
ans.append(x[0])
return dr2(a, x[1:], ans)
5. Reverse Recursive
print(rev2([10,20,30, 40,50]))
# rev2( [10, 20, 30, 40, 50] , [] )
# rev2( [20, 30, 40, 50] , [10] )
# rev2( [30, 40, 50] , [20, 10] )
# rev2( [40, 50] , [30, 20, 10] )
# rev2( [50] , [40, 30, 20, 10] )
# rev2( [] , [50, 40, 30, 20, 10] )
[50, 40, 30, 20, 10]
6. Factorial Recursive
print(factr2(5))
# 5 1
# 4 5
# 3 20
# 2 60
# 1 120
# 0 120
120
Processing Nested/Embedded Lists
Flat list can be used for Linear Data Structures
[10, 20, 70, 80, 30, 50, 60, 40]
Nested lists can be user for Multi-Dimensional Data Structures like tables
[[10, 20], [70, 80], [30, 50], [60, 40]]
def cr2e(a,c=0):
print(a, '\t', c)
if a == []:
return c
elif type(a[0]) is list:
c1 = cr2e(a[0])
return cr2e(a[1:],c+c1)
else:
return cr2e(a[1:],c+1)
print(cr2e([10,[20,30],40]))
# [10, [20, 30], 40] 0
# [[20, 30], 40] 1
# [20, 30] 0
# [30] 1
# [] 2
# [40] 3
# [] 4
4
print(sr2e([10,[20,30],40]))
# [10, [20, 30], 40] 0
# [[20, 30], 40] 10
# [20, 30] 0
# [30] 20
# [] 50
# [40] 60
# [] 100
100
def mre(a,x):
print(a, ' ',x)
if x == []:
return False
elif a == x[0]:
return True
elif type(x[0]) is list:
return mre(a,x[0]) or mre(a,x[1:])
else:
return mre(a,x[1:])
print(rev2e([10,20,[40,50,30],60]))
# rev2e( [10, 20, [40, 50, 30], 60] , [] )
# rev2e( [20, [40, 50, 30], 60] , [10] )
# rev2e( [[40, 50, 30], 60] , [20, 10] )
# rev2e( [40, 50, 30] , [] )
# rev2e( [50, 30] , [40] )
# rev2e( [30] , [50, 40] )
# rev2e( [] , [30, 50, 40] )
# rev2e( [60] , [[30, 50, 40], 20, 10] )
# rev2e( [] , [60, [30, 50, 40], 20, 10] )
[60, [30, 50, 40], 20, 10]
---------------------------END -------------------