PROGRAM-1
Write a program which is menu driven using functions to
perform bubble sort and insertion sort.
def insert(l):
for i in l:
j=l.index(i)
while j>0:
if l[j-1]>l[j]:
l[j],l[j-1]=l[j-1],l[j]
else:
break
j=j-1
return l
def bubble(l):
n=len(l)
for i in range(n-1):
for j in range(n-1-i):
if l[j]>l[j+1]:
l[j],l[j+1]=l[j+1],l[j]
return l
ans="Yes"
while ans=="Yes":
l=[]
a=int(input("enter the limit"))
for i in range(a):
b=int(input("enter element"))
l.append(b)
print("1.bubble, 2.insertion")
ch=int(input("enter choice"))
if ch==1:
print(bubble(l))
elif ch==2:
print(insert(l))
else:
print("invalid")
ans=input("Do you want to continue. Yes/No")
Output
PROGRAM-2
Write a menu driven program using functions to
perform i) Linear search ii) Binary Search
def linear_search(lst, key):
for i in range(len(lst)):
if lst[i] == key:
print("Found at", i, "position")
break
else:
print("Not found")
def binary_search(lst, key):
lst.sort()
low = 0
high = len(lst) - 1
while low <= high:
mid = (low + high) // 2
if key == lst[mid]:
print("Found at:", mid)
break
elif key < lst[mid]:
high = mid - 1
elif key > lst[mid]:
low = mid + 1
else:
print("Not found")
ans = "Yes"
while ans == "Yes":
lst = []
n = int(input("Enter the limit: "))
for i in range(n):
val = int(input("Enter the element: "))
lst.append(val)
print("1. Linear Search")
print("2. Binary Search")
ch = int(input("Enter choice: "))
key = int(input("Enter the element to be searched: "))
if ch == 1:
linear_search(lst, key)
elif ch == 2:
binary_search(lst, key)
else:
print("Invalid choice")
ans = input("Do you want to continue? Yes/No: ")
Output
Program 3
Write a program to display the unique and
duplicated items in a list.
def unique(l):
u=[]
v=[]
for i in l:
c=0
for j in l:
if i==j:
c+=1
if c==1:
u.append(i)
else:
if i not in v:
v.append(i)
print("unique elements list=",u)
print("repeated elements list=",v)
ans="yes"
while ans=="yes":
l=list(eval(input("enter the elements")))
unique(l)
ans=input("do you want to continue")
Output
Program 4
Write a program to accept a dictionary D and then
display the elements in opposite mapping.
def rev(d):
d2 = {}
for k in d:
v = d[k]
d2[v] = k
print("Reversed dict:", d2)
ans = "yes"
while ans == "yes":
d1 = {}
n = int(input("Enter limit: "))
for i in range(n):
k = input("Enter key: ")
v = input("Enter value: ")
d1[k] = v
rev(d1)
ans = input("Do you want to continue? ")
Output
Program 5
Write a program to exchange first half of the elements in
a list with second half.
def change_list(l):
size = len(l)
if size % 2 == 0:
mid = size // 2
pos = 0
for i in range(mid):
l[i], l[mid + pos] = l[mid + pos], l[i]
pos += 1
else:
mid = size // 2
pos = 1
for i in range(mid):
l[i], l[mid + pos] = l[mid + pos], l[i]
pos += 1
return l
again = "yes"
while again == "yes":
l = list(eval(input("Enter elements: ")))
result = change_list(l)
print("Changed list:", result)
again = input("Do you want to continue? ")
Output
Program 6
Write a program generating a random number between 1-
6 simulating a dice roll.
import random
def dice():
x=random.randint(1,6)
print(x)
i="yes"
while i=="yes":
dice()
i=input('Do you want to continue:')
Output
Program 7
Write a menu driven program to accept name and
phone number of employees, display the contents
in the dictionary and search a number based on the
name.
def read(d):
nme=input("Enter name to be searched:")
for i in d:
if i==nme:
print(d[i])
break
else:
print('not found')
d={}
n=int(input("Enter the limit:"))
for i in range(n):
a=input("Enter name;")
ph=int(input("Enter the phone number:"))
d[a]=ph
ans='yes'
while ans=="yes":
print("1.display , 2.search")
ch=int(input("Enter the choice:"))
if ch==1:
print(d)
elif ch==2:
read(d)
else:
print("invalid choice:")
ans=input("do you want to continue?")
Output