Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

PROGRAM 4 MENU DRIVEN CODE – LIST/STRING

AIM:
Using a menu driven code, perform the following, menu options:
i) Binary Search in a list of integers ii) Reversing a string iii)Exit

PROGRAM CODING
def rev_str(s):
s1=''
if len(s)== 0:
return s
else:
for i in range(len(s)-1,-1,-1):
s1+=s[i]
return s1

def binary_search(alist,num):
start=0
end=len(alist) - 1
mid = (start + end)//2
while start<=end:
if num==alist[mid]:
print(num,"is found at ",mid+1,"position")
break
if num>=alist[mid]:
start=mid+1
mid=(start+end)//2
else:
end=mid-1
mid=(start+end)//2
else:
print(num,"is not present in the given list")

print(''' MENU
1.Binary Search in a list of integers
2.Reversing a string
3.Exit''')
while True:
ch=int(input('Enter your choice : '))
if ch==1:
alist=eval(input('Enter numbers for the list in ascending order: '))
n=int(input("Enter the number to be searched:"))
binary_search(alist,n)
elif ch==2:
str=input("Enter a string")
print("original string",str)
print("Reversed string",rev_str(str))
elif ch==3:
break
else:
print('Invalid choice')
OUTPUT
MENU
1.Binary Search in a list of integers
2.Reversing a string
3.Exit
Enter your choice : 1
Enter numbers for the list in ascending order: [4,56,78,83,90]
Enter the number to be searched83
83 is found at 4 position
Enter your choice : 1
Enter numbers for the list in ascending order: [4,56,78,83,90]
Enter the number to be searched99
99 is not present in the given list
Enter your choice : 2
Enter a stringamazon
original string amazon
Reversed string nozama
Enter your choice : 3

You might also like