The document contains 10 questions related to Python programming concepts like searching algorithms, sorting algorithms, recursion, pattern printing, and file handling. It provides code snippets to demonstrate linear search, binary search, bubble sort, selection sort, insertion sort, reversing a list, calculating power using recursion, converting decimal to binary using recursion, generating Fibonacci series using recursion, and printing a pattern using recursion. It also lists important Python topics like functions, recursion, map/filter/lambda functions, classes/objects, exception handling, and file handling for a coding interview or viva examination.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
42 views4 pages
Question Bank
The document contains 10 questions related to Python programming concepts like searching algorithms, sorting algorithms, recursion, pattern printing, and file handling. It provides code snippets to demonstrate linear search, binary search, bubble sort, selection sort, insertion sort, reversing a list, calculating power using recursion, converting decimal to binary using recursion, generating Fibonacci series using recursion, and printing a pattern using recursion. It also lists important Python topics like functions, recursion, map/filter/lambda functions, classes/objects, exception handling, and file handling for a coding interview or viva examination.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4
#Q1).
Write a python code and show the leaner search
def linearSeach(array,n,key): #looping with all the elements by short hand loop where (n//2+1) for i in range(n//2+1): #check if the key found on particular index if(array[i]==key or array[n-1-i]==key): #return to elements return i if array[i]==key else n-1-i return -1
#List elements array=list(map(int,input('Enter the elements: ').split(','))) #searching the required key key=int(input('Enter the key to be searched: '))
#considering total length
n=len(array) #Function call on successful element search found=linearSeach(array,n,key)
#if key not found
if(found==-1): print("Key not found",-1) else: print("Key found at index: ",found)
#Q2.) Write a python code and show the Binary search
def binarySearch(array, key, low, high): # Repeat until the pointers low and high meet each other while low <= high: mid = low + ((high - low)//2) if array[mid] == key: return mid elif array[mid] < key: low = mid + 1 else: high = mid - 1 return -1 array=list(map(int,input('Enter the elements: ').split(','))) key = int(input("Enter the element to be searched:")) result = binarySearch(array, key, 0, len(array)-1) if result != -1: print("Element is present at index " + str(result)) else: print("Not found",-1)
#Q3). Write a python code and show the Bubblesort
def bubbleSort(array): # loop to access each array element for i in range(len(array)): # loop to compare array elements f=0 for j in range(0,len(array)-i-1): # compare two adjacent elements # change > to < to sort in descending order if(array[j]>array[j+1]): # swapping elements if elements # are not in the intended order temp=array[j] array[j]=array[j+1] array[j+1]=temp f=1 if not f: break array=list(map(int,input('Enter the elements: ').split(','))) bubbleSort(array) print('Sorted Array in Ascending Order:') print(array)
#Q4). Selection sort in Python
def selectionSort(array, size): for step in range(size): for i in range(step+1,size): # to sort in descending order, change > to < in this line # select the minimum element in each loop if (array[i]<array[step]): # put min at the correct position array[step],array[i]=array[i],array[step] return data =list(map(int,input('Enter the elements: ').split(','))) size = len(data) selectionSort(data,size) print('Sorted Array in Ascending Order:') print(data)
#Q5). Write a python code and show the Insertion sort
def insertionSort(array): for step in range(1, len(array)): key = array[step] j = step - 1 # Compare key with each element on the left of it until an element smaller than it is found # For descending order, change key<array[j] to key>array[j]. while(j>=0 and key<array[j]): array[j + 1] = array[j] j=j-1 # Place key at after the element just smaller than it. array[j+1]=key
data=list(map(int,input('Enter the elements: ').split(',')))
insertionSort(data) print('Sorted Array in Ascending Order:') print(data)
#Q6). Reverse the given list
def reverseList(a,n): i=0 while(i<n-1-i): a[i],a[n-1-i]=a[n-1-i],a[i] i+=1 a=list(map(int,input("read a list of no using :").split(','))) print("before reverse list elements: ",a) reverseList(a,len(a)) print("after reverse list elements: ",a)
#Q7). Reccurssion power of number without ** and * using recurssion
def mul(x,y): if(not y): return 0 #returing the first multiplication of the given numbers return x+mul(x,y-1) def pow(a,b): if(not b): return 1 #returning the power of number by calling the function again return mul(a,pow(a,b-1)) #a is first input #b is second input print(pow(int(input("a: ")),int(input("b :"))))
#Q8).Decimal to binary using recurssion, by reading input
def binary(n): if(n>1): binary(n//2) print(n%2,end='') decimal=int(input("enter a number: ")) binary(decimal)
#Q9).fibo like a pal no using rec,by reading a input