0% found this document useful (0 votes)
108 views3 pages

Program 4 Menu Driven Code - List/String

This document presents a menu driven program with options for binary search of an integer in a list, reversing a string, and exiting. It defines functions for binary search and string reversal. The main program loop displays the menu and performs the selected operation, either searching a user-input list for a number and printing the result, reversing a user-input string and printing both, or exiting the loop. Sample output demonstrates searching lists where the number is and isn't found and reversing a string.

Uploaded by

vimala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views3 pages

Program 4 Menu Driven Code - List/String

This document presents a menu driven program with options for binary search of an integer in a list, reversing a string, and exiting. It defines functions for binary search and string reversal. The main program loop displays the menu and performs the selected operation, either searching a user-input list for a number and printing the result, reversing a user-input string and printing both, or exiting the loop. Sample output demonstrates searching lists where the number is and isn't found and reversing a string.

Uploaded by

vimala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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