Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Python File Handlings

Uploaded by

dhruvtiwarix2910
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python File Handlings

Uploaded by

dhruvtiwarix2910
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

MOTHR TERESA SENIOR SECONDARY COED SCHOOL

SUBJECT – COMPUTER SCIENCE


NOTES CLASS XII CHAPTER – RECURSION

Flow of Execution in function –

When you read a program, don't read from top to bottom .In function instead of going to the next statement,
the flow of execution in function jumps to the first line of the called function, executes all the statements there,
and then comes back to pick up where it left off.

Recursion:

Recursion is a way of programming or coding a problem, in which a function calls itself again and again one or
more times in its body. Usually, it is returning the return value of this function call. If a function definition fulfills
the condition of recursion, we call this function a recursive function.

Conditions for implement Recursion:

1) A recursive function calls itself. As you you'd imagine such a process would repeat indefinitely if not stopped
by some condition. This condition is known as base condition. A base condition is must in every recursive
programs otherwise it will continue to execute forever like an infinite loop. There must be if condition in the
recursive routine.This if specifies terminating condition of recursion.

2) 2) In the next condition where function call itself again again in program is called recursive condition.

Advantages of Python Recursion

1)Reduces unnecessary calling of function, thus reduces length of program.


2)Big and complex iterative solutions are easy and simple with Python recursion.
Disadvantages of Python Recursion
1)Slow.
2)Logical but difficult to trace and debug.
3)Requires extra storage space. For every recursive calls separate memory is
allocated for the variables.
Recursive functions often throw a Stack Overflow Exception when processing or operations are too large

How recursion work in python:

Recursion is implement in any code by diving the problem in following two part:-
1) Terminating part(Base condition):-When a function return a value directly.
2) Recursive part:-which contain one or more recursive calls on smaller parts of the problem.
For example:-

To calculate power of an input number using recursion:

def power(x,n):
if n==1: base condition
return 1
else: recursive part
return x*power(x,n-1)
x=2
n=4
print(power(2,4))
#function to display Fibonic series using Recursion
def rfibo(n):
if n<=1:
return n
else:
return(rfibo(n-1)+rfibo(n-2))
n=int(input("term required in series"))
for i in range(n):
print(rfibo(i),end=' ')
Explanation:
In give example the base condition of the function is execute when no is less than or equal to 1.Otherwise the
function call recursively with the argument as first no minus 1 added to the function called recursively with the
argument to the second no minus 2.

#write a function to find the sum of n natural no

def nsum(n):
if n<=1:
return 1
else:
return n+nsum(n-1)
n=int(input(“enter a no”))
Print(“sum of n natural no”,nsum(n))

Recursion Vs. Iteration:Recursion and Iteration are interrelated concepts.Therefore, we can say the problem that
can be solved through iteration can bo solve using recursion as well and vice versa.But the basic difference
between loop and recursion is when loop is execute it use the same memory location variable and code. On other
hand recursive function use fresh memory space for each recursive call.

So in case of recursion extra memory space use in RAM by each recursive call to a function
Binary Search:

Binary Search is Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval
covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow
the interval to the lower half. Otherwise narrow it to the upper half.

Binary Search Algorithm


Step 1 - Read the search element from the user.
Step 2 - Find the middle element in the sorted list.
Step 3 - Compare the search element with the middle element in the sorted list.
Step 4 - If both are matched, then display "Given element is found!!!" and terminate the function.
For example:-
first=0 and last=size-1=7

10 20 `24 30 `58 62 71 89
0 1 2 3 4 5 6 7

mid=(first+last)//2

# If searched value is 25,25<mid (30) then searching start from left to right again it find mid value by adding first
and last divided by 2 in this situation first=0 and last=mid-1 again find mid and start searching till search element
find at mid position. In this search element return only mid.
# If mid value is 42,42<mid(30) then searching start from right to left in this situation first=mid+1 and last=7

# Python Program for binary search.

def bsearch(L,f,l,ele):
x=len(L)
for a in range(x):
mid=(f+l)//2
if L[mid]>ele:
l=mid-1
elif L[mid]<ele:
f=mid+1
elif L[mid]==ele:
return mid
else:
return -1
L=[10,20,24,36,48]
ele=int(input("enter an ellement to be search"))
f=0
l=len(L)-1
bs=bsearch(L,f,l,ele)
if bs!=-1:
print("element found at",bsearch(L,f,l,ele))
else:
print("not found")

# Python Program for recursive binary search.

def bsearch (L, f, l, x):


if (l<f):
return 0
else:
mid=(f+l)//2
if L[mid]>x:
return bsearch(L,f,mid-1,x)
elif L[mid]<x:
return bsearch(L,mid+1,l,x)
elif L[mid]==x:
return mid
else:
return -1
L=[10,20,36,42,58]
x=int(input("enter search element"))
f=0
l=4
bs=bsearch(L,f,l,x)
if bs!=-1:
print("element found at index",bs)
else:
print("element not found")

You might also like