Python File Handlings
Python File Handlings
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.
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.
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:-
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.
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.
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
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")