Q2 Find minimum element from the list.def smallkth(a,lb,ub,k): for i in range(lb,ub): for j in range(lb,ub-i): if(a[j]>a[j+1]): temp=a[j] a[j]=a[j+1] a[j+1]=temp print('list is ',a,'and ',k,'smallest element is', a[k-1]) a=[10,2,3,4,67,8,9,34] ub=len(a) k=int(input("enter kth smallest element you want")) smallkth(a,0,ub-1,k) Output >>> ==================== RESTART: C:/Users/Dell/Desktop/ss.py ==================== enter kth smallesest element you want3 list is [2,3,4,8,9,10,34,67] and 3 smallest element is 4
Q3.Program to find maximum element from list.def fm(a,lb,ub): for i in range(lb,ub): mi=a[lb] if(a[i]<=mi): mi=a[i] print(mi) a=[12,3,4,56,7,89,1] ub=len(a) fm(a,0,ub) Output >>> ==================== RESTART: C:/Users/Dell/Desktop/ss.py ==================== 1
Q4.Program to find kth largest element from list.def fmax(a,lb,ub): for i in range(lb,ub): mi=a[lb] if(a[i]>mi): mi=a[i] print(mi) a=[12,3,4,56,7,89,1] ub=len(a) fmax(a,0,ub) Output >>> ==================== RESTART: C:/Users/Dell/Desktop/ss.py ==================== 12
Q5.Program to find list in reverse order.def smallkth(a,lb,ub,k): for i in range(lb,ub): for j in range(lb,ub-i): if(a[j]>a[j+1]): temp=a[j] a[j]=a[j+1] a[j+1]=temp a.reverse() print('list is',a,'and',k ,'largest element',a[k-1]) a=[10,2,3,4,67,8,9,34] ub=len(a) k=int(input("enter kth largest element you want")) smallkth(a,0,ub-1,3) Output >>> ==================== RESTART: C:/Users/Dell/Desktop/ss.py ==================== enter kth largest element you want2 list is [67, 34, 10, 9, 8, 4, 3, 2] and 3 largest element 10
def rlist(a,lb,ub): for i in range(ub,lb,-1): print(a[i]) a=[10,2,3,4,67,8,9,34] ub=len(a) rlist(a,-1,ub-1) Output >>> ==================== RESTART: C:/Users/Dell/Desktop/ss.py ==================== 34 9 8 67 4 3 2 10
0 Comments