Posts

Replace all 0's with 5

Image
Given a number N. The task is to complete the function convertFive() which replace all zeros in the number with 5 and returns the number. Input: The first line of input contains an integer T, denoting the number of testcases. Then T testcases follow. Each testcase contains a single integer N denoting the number. Output: The function will return integer where all zero's are converted to 5. User Task: Since this is a functional problem you don't have to worry about input, you just have to complete the function convertFive(). Constraints: 1 <= T <= 103 1 <= N <= 104 Example: Input 2 1004 121 Output 1554 121 Explanation: Testcase 1:  At index 1 and 2 there is 0 so we replace it with 5. Testcase 2: There is no ,0 so output will remain same. Answer:- Program:- class Solution:     def convertFive(self,n):         return str(n).replace("0","5") if __name__=='__main__':     t=int(input())     for i in range(t):         n=int(input())         prin

Selection sort program in python

Image
           WRITE A PROGRAM FOR SELECTION SORT def Selection_Sort(MyList):     #i - outer loop     #j - Inner loop     #k - index of the smallest element          for i in range(len(MyList)-1):         k=i   #i th element is assumed to be smallest         for j in range(i+1,len(MyList)):             if(MyList[j]<MyList[k]):                 k=j         if (k!=i):             temp=MyList[i]             MyList[i]=MyList[k]             MyList[k]=temp MyList=[12,34,56,2,6,44,32,67,89] print("Element before swapping:") print(MyList) Selection_Sort(MyList) print("Elements After Sorting:") print(MyList) PROGRAM:-  OUTPUT:-

BUBBLE SORT IN PYTHON

Image
WRITE A PROGRAM TO IMPLEMENT BUBBLE SORT. def Bubble_Sort(MyList):     for i in range(len(MyList)-1,0,-1):         for j in range(i):             if MyList[j]>MyList[j+1]:                 temp,MyList[j]=MyList[j],MyList[j+1]                 MyList[j+1]=temp MyList=[30,50,45,23,20,90,79] print("Elements of List Before Sorting:",MyList) Bubble_Sort(MyList) print('Element of List After Sorting:',end='') print(MyList)      EXPLANATION:- In the above program,the unsorted list is declared and initialized. The same list is passed as an argument to the function bubble sort.The list slicing operation is used to iterate through the loop. Each element is compared with its adjacent element and interchanged if the first element is greater than the second. Swapping of elements is done using Python's simultaneous assignment as shown. PROGRAM:- OUTPUT:- Also see:- PROGRAM FOR BINARY SEARCH LINEAR SEARCH PROGRAM IN PYTHON

BINARY SEARCH IN PYTHON

Image
WRITE A PROGRAM FOR BINARY SEARCH def Binary_Search(MyList,key):     low=0     high=len(MyList)-1     while low<=high:         mid=(low+high)//2  #find the middle index         if MyList[mid]==key:  #if entered key matches the mid index element             return mid        #return mid index         elif key>MyList[mid]: #else if key is greater             low=mid+1         else:             high=mid-1     return -1          #if no match,return -1 MyList=[10,20,30,40,45,56,69,88] print(MyList) key=(eval(input("Enter the number to search:"))) x=Binary_Search(MyList,key) if(x==-1):     print(key,"is not present in the list") else:     print("The Element",key,"is found at the position",x+1) EXPLANATION:- In the above program,we have defined the list with the elements [10,20,30,40,45,56,69,88]. The number to be searched is prompted from user.The list and the number to be searched both are passes as parameters to the function.In each iteration,

LINEAR SEARCH PROGRAM IN PYTHON

Image
WRITE A PROGRAM TO SEARCH AN ELEMENT FROM A LIST IN PYTHON def Linear_Search(My_List,key):     for i in range(len(My_List)):         if(My_List[i]==key):             print(key,"is found at index ",i)             return i             break     return -1 My_List=[12,23,45,67,89] print("Constents of List are as follows:") print(My_List) key=(int(input("Enter the number to be searched:"))) L1=Linear_Search(My_List,key) if(L1!=-1):     print(key,"is found at position",L1+1) else:     print(key,"is not found in the List") EXPLANATION:-  In the above program,we have defined the function called Linear_Search(). The list and element to be searched, i.e the key ,is passed to the function. Th comparison starts from the first element of the list.The comparison goes on sequentially till the key element matches the element present within the list is exhausted without a match being found. PROGRAM:- OUTPUT:- ALSO SEE:- BINARY SEARCH IN PYTHON