Download Functions and Recursion in Python and more Lecture notes Web Programming and Technologies in PDF only on Docsity!
Functions and Recursion in Python
Data and File Structures Laboratory
http://www.isical.ac.in/~dfslab/2020/index.html
Parameter passing
def function1 (x) :
x = 10
return
def function2 (x) :
x = x + 1
return
def function3 (l) :
l = [ 10 ]
return
def function4 (l) :
l[0] = 10
return
def function5 (l) :
l = l + l
print(l)
return
def function6 (l) :
l.extend(l)
return
Parameter passing in Python
Call by reference
Immutable arguments (integers, strings, tuples): passed by value or
reference (doesn’t matter)
Mutable arguments : passed by reference
in place changes reflected in caller
if the name is reassigned, it points to a different object
Parameter passing in Python
Call by reference
Immutable arguments (integers, strings, tuples): passed by value or
reference (doesn’t matter)
Mutable arguments : passed by reference
in place changes reflected in caller
if the name is reassigned, it points to a different object
>>> x = 10
>>> id(x)
>>> y = 10
>>> id(y)
>>> y = y+
>>> id(y)
x = a
x
x = b
x
x = x + 1
x.inplace_operation()
vs.
x now points to a different object
Recursive vs. iterative implementations
def factorial(n) :
# Not recommended!
if n < 0 : return -
prod = 1
while n > 0 :
prod *= n
n = n - 1
return prod
def factorial(n) : # Recursive implementation
if n < 0 : return -1 # Error condition if n == 0 : return 1 # Base case return n * factorial(n-1) # Recursive call
Z curve
Problem statement
Consider a 2-D matrix of size 2 m^ × 2 m. The entries of the matrix are, in row-major order, 1 , 2 , 3 ,... , 22 m. Print the entries of the matrix in Z-curve
order (as shown in the picture below).
Z curve: code
def z_curve(matrix, top_left_row, top_left_column, bottom_right_row, bottom_right_column) :
Base case
if top_left_row == bottom_right_row and top_left_column == bottom_right_column : print("%d" % matrix[top_left_row][top_left_column], end=" ") return
TLC
TLR
BRR
BRC
Z curve: code
def z_curve(matrix, top_left_row, top_left_column, bottom_right_row, bottom_right_column) :
Base case
if top_left_row == bottom_right_row and top_left_column == bottom_right_column : print("%d" % matrix[top_left_row][top_left_column], end=" ") return
TLC BRC
TLR TLR
BRR BRR
TLC BRC
Z curve: code
def z_curve(matrix, top_left_row, top_left_column, bottom_right_row, bottom_right_column) :
Base case
if top_left_row == bottom_right_row and top_left_column == bottom_right_column : print("%d" % matrix[top_left_row][top_left_column], end=" ") return
TLC (TLC + BRC)//2 (TLC + BRC)//2 + 1 BRC
TLR TLR
(TLC + BRC)//2 (TLC + BRC)//2 + 1
BRR BRR
TLC (TLC + BRC)//2 (TLC + BRC)//2 + 1 BRC
Z curve: code
def z_curve(matrix, top_left_row, top_left_column, bottom_right_row, bottom_right_column) :
Base case
if top_left_row == bottom_right_row and top_left_column == bottom_right_column : print("%d" % matrix[top_left_row][top_left_column], end=" ") return
TLC (TLC + BRC)//2 (TLC + BRC)//2 + 1 BRC
TLR TLR
(TLR + BRR)//2 (TLC + BRC)//2 (TLC + BRC)//2 + 1
(TLR + BRR)//2 + 1
BRR BRR
TLC (TLC + BRC)//2 (TLC + BRC)//2 + 1 BRC
Z curve: code
# lower-right sub-square
z_curve(matrix,
(top_left_row + bottom_right_row)//2 + 1,
(top_left_column + bottom_right_column)//2 + 1,
bottom_right_row,
bottom_right_column)
return
Z curve: code
# lower-right sub-square
z_curve(matrix,
(top_left_row + bottom_right_row)//2 + 1,
(top_left_column + bottom_right_column)//2 + 1,
bottom_right_row,
bottom_right_column)
return
N = 2**
i = 1; j = 0
A = [ [ x for x in range(jN+i, jN+i+N) ] for j in range(N) ]
z_curve(A,0,0,N-1,N-1); print()
Permutations: code
def permute(A) :
n = len(A)
if n < 2: return [ A ]
# Need recursion for n >=
permlist = [ ]
for p in permute(A[1:]) :
for position in range(n) :
p.insert(position, A[0])
permlist.append(p.copy())
p.pop(position)
return permlist
Problems – I
1. Towers of Hanoi: see
https://en.wikipedia.org/wiki/Tower_of_Hanoi
CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=
- Write a recursive function with prototype int C(int n, int r); to
compute the binomial coefficient using the following definition:
( n r
)
=
( n − 1 r
)
( n − 1 r − 1
)
Supply appropriate boundary conditions.