Download Lecture 8

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Lecture 8: ES 102
The Magic of Recursion
Introduction to Computing
IIT Gandhinagar, India
Oct, 2013
logo
Bireswar,Shivakumar (IIT Gandhinagar)
Lecture 8
Oct, 2013
1 / 15
What happens when things change inside a function?
Immutable “things” (objects) cannot be changed inside a function
(numbers, tuples, strings).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def f u n c 1 ( i ) :
i =5
print " Inside function 1 " , i
def f u n c 2 ( n ) :
i=n+5
print " Inside function 2 " , i
i =42
print " Before call " , i
func1 ( i )
func2 ( i )
print " After call " , i
def f u n c 3 ( s t r ) :
s t r=" abc "
s t r=" lmn "
print " Before call " , s t r
func3 ( s t r )
print " After call " , s t r
logo
Bireswar,Shivakumar (IIT Gandhinagar)
Lecture 8
Oct, 2013
2 / 15
What happens when things change inside a function?
Mutable “things” (objects) can be changed inside a function (lists,
objects).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def f u n c 4 ( l s t ) :
l s t . append ( 4 )
print " Inside function 4 " , l s t
def f u n c 5 ( l s t ) :
l s t=l s t +[5]
print " Inside function 5 " , l s t
l s t =[1 ,2 ,3]
print " Before call " , l s t
func4 ( l s t )
print " After call " , l s t
print " Before calling func5 " , l s t
func5 ( l s t )
print " After call " , l s t
logo
Bireswar,Shivakumar (IIT Gandhinagar)
Lecture 8
Oct, 2013
3 / 15
The id() Function
The id() function gives the reference of an entity (object).
It could be used to check that the reference value of an argument is
passed to a function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def f u n c 4 ( l s t ) :
l s t . append ( 4 )
print i d ( l s t )
def f u n c 5 ( l s t ) :
print i d ( l s t )
l s t=l s t +[5]
print " inside func5 " , i d ( l s t )
l s t =[1 ,2 ,3]
print " Before call " , i d ( l s t )
func4 ( l s t )
print " After call " , i d ( l s t )
print " Before calling func5 " , i d ( l s t )
func5 ( l s t )
print " After call " , i d ( l s t )
logo
Bireswar,Shivakumar (IIT Gandhinagar)
Lecture 8
Oct, 2013
4 / 15
Another Example
1
2
def f u n c 4 ( n ) :
print i d ( n )
3
4
5
6
i =234
print i d ( i )
func4 ( i )
logo
Bireswar,Shivakumar (IIT Gandhinagar)
Lecture 8
Oct, 2013
5 / 15
A Function to Compute Factorial
1
2
3
4
5
def f a c t o r i a l ( n ) :
v a l =1
f o r i in r a n g e ( 1 , n +1):
v a l ∗= i
return v a l
6
7
print f a c t o r i a l ( 5 )
logo
Bireswar,Shivakumar (IIT Gandhinagar)
Lecture 8
Oct, 2013
6 / 15
A Mathematical Definition of Factorial
Definition
factorial(n) =
1
n.factorial(n − 1)
if n ≤ 1
otherwise
logo
Bireswar,Shivakumar (IIT Gandhinagar)
Lecture 8
Oct, 2013
7 / 15
Implementation using Recursion
1
2
3
4
5
def f a c t o r i a l ( n ) :
i f n==1:
return 1
else :
return n∗ f a c t o r i a l ( n−1)
6
7
print f a c t o r i a l ( 5 )
logo
Bireswar,Shivakumar (IIT Gandhinagar)
Lecture 8
Oct, 2013
8 / 15
Recursion
When a function is called inside itself it is called recursion.
Recursion is very similar to mathematical induction. In induction we
have base case(s) and inductive step.
A typical recursive function also has base case(s) and recursive case.
In the factorial example above, the base case was n = 1 and the
inductive case was n > 1.
logo
Bireswar,Shivakumar (IIT Gandhinagar)
Lecture 8
Oct, 2013
9 / 15
Exercise
Write a program to find 15 + 25 + · · · + n5 without loop.
logo
Bireswar,Shivakumar (IIT Gandhinagar)
Lecture 8
Oct, 2013
10 / 15
Exercise
Write a program to find 15 + 25 + · · · + n5 without loop.
Try to give a mathematical definition similar to factorial.
logo
Bireswar,Shivakumar (IIT Gandhinagar)
Lecture 8
Oct, 2013
10 / 15
Exercise
Write a program to find 15 + 25 + · · · + n5 without loop.
Try to give a mathematical definition similar to factorial.
Definition
S(n) =
1
n5 + S(n − 1)
if n ≤ 1
otherwise
logo
Bireswar,Shivakumar (IIT Gandhinagar)
Lecture 8
Oct, 2013
10 / 15
Sum of 5th powers
1
2
3
4
5
def f p ( n ) :
i f n==1:
return 1
else :
return n∗∗5+ f p ( n−1)
6
7
print f p ( 3 )
logo
Bireswar,Shivakumar (IIT Gandhinagar)
Lecture 8
Oct, 2013
11 / 15
Number of ways of picking k elements from n elements

 1
n
C(n, k) =

C(n − 1, k − 1) + C(n − 1, k)
if n = k
if k = 1
otherwise
logo
Bireswar,Shivakumar (IIT Gandhinagar)
Lecture 8
Oct, 2013
12 / 15
Number of ways of picking k elements from n elements
1
2
3
4
5
6
7
def c h o o s e ( n , k ) :
i f n==k :
#b a s e c a s e 1
return 1
e l i f k==1: #b a s e c a s e 2
return n
else :
return c h o o s e ( n −1,k−1)+ c h o o s e ( n −1 , k )
8
9
print c h o o s e ( 5 , 3 )
logo
Bireswar,Shivakumar (IIT Gandhinagar)
Lecture 8
Oct, 2013
13 / 15
Exercise
Find the minimum number in a list of numbers.
logo
Bireswar,Shivakumar (IIT Gandhinagar)
Lecture 8
Oct, 2013
14 / 15
Exercise
Find the minimum number in a list of numbers.
1
2
3
4
5
6
7
8
def min ( l s t ) :
min= l s t [ 0 ]
i n d e x =0
f o r i in r a n g e ( l e n ( l s t ) ) :
i f l s t [ i ]<min :
min= l s t [ i ]
i n d e x=i
return min , i n d e x
9
10
l s t =[6 ,4 ,3 ,9 ,2 ,7]
11
12
13
m i n l , i n d l=min ( l s t )
print m i n l , i n d l
logo
Bireswar,Shivakumar (IIT Gandhinagar)
Lecture 8
Oct, 2013
14 / 15
Sorting: Insertion Sort
The Algorithm:
I
I
Assume that the list is already sorted up to index i − 1.
Insert the ith element in the correct position in the sorted list.
logo
Bireswar,Shivakumar (IIT Gandhinagar)
Lecture 8
Oct, 2013
15 / 15
Sorting: Insertion Sort
The Algorithm:
I
I
Assume that the list is already sorted up to index i − 1.
Insert the ith element in the correct position in the sorted list.
1
2
3
4
5
6
7
8
9
10
def i s o r t ( l s t ) :
n=l e n ( l s t )
f o r i in r a n g e ( 1 , n ) :
j=i −1
temp= l s t [ i ]
while j >=0 and l s t [ j ]>temp :
l s t [ j +1]= l s t [ j ]
j −=1
l s t [ j +1]=temp
11
12
13
14
l s t =[21 ,10 ,9 ,7 ,23 ,21 ,17 ,2 ,11]
isort ( lst )
print l s t
logo
Bireswar,Shivakumar (IIT Gandhinagar)
Lecture 8
Oct, 2013
15 / 15
Related documents