//Linear search arr=[] found=-1 n=int(input("Enter number of elements: ")) for i in range(n): ele=int(input("Enter element: ")) arr.append(ele) key=int(input("Enter key: ")) for i in range(n): if(arr[i]==key): found=i break if(found!=-1): print("Element found at index: ",found) else: print("Element not found.") //Merge two lists list1=[] list2=[] newlist=[] def mergi(): i=0 j=0 while(i<len(list1) and j<len(list2)): if(list1[i]<list2[j]): newlist.append(list1[i]) i+=1 elif(list1[i]>list2[j]): newlist.append(list2[j]) j+=1 elif(list1[i]==list2[j]): newlist.append(list1[i]) i+=1 j+=1 while(i<len(list1)): newlist.append(list1[i]) i+=1 while(j<len(list2)): newlist.append(list2[j]) j+=1 n=int(input("Enter number of elements in list 1: ")) for i in range(n): ele=int(input("Enter element: ")) list1.append(ele) n=int(input("Enter number of elements in list 2: ")) for i in range(n): ele=int(input("Enter element: ")) list2.append(ele) mergi() print(newlist)
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter