Example:3
Write a Python program to create a list further take input for an element to search, check and print whether the element is present or not using linear search(using functions)
Sol:
#create an empty list #global blank list n=[] def create_list(): while True: a=int(input("Enter the element to add in list ")) n.append(a) ch=input("Like to add more elements (y/n) ") if(ch=="y" or ch=="Y"): continue else: break def display_list(): # 1st method to display list print("List is ") print(n) # 2nd method to display list for i in n: print(i,end=' ') def search_list(item): i = 0 z = False while(i < len(n)): if(n[i] == item): z = True break i = i + 1 if(z==True): print("element found ") else: print("element not found") #function calling create_list() display_list() item=int(input("\n\nEnter element to search ")) search_list(item)
Output:
case 1:
Enter the element to add in list 10
Like to add more elements (y/n) y
Enter the element to add in list 20
Like to add more elements (y/n) y
Enter the element to add in list 30
Like to add more elements (y/n) n
List is
[10, 20, 30]
10 20 30
Enter element to search 20
element found
case 2:
Enter the element to add in list 10
Like to add more elements (y/n) y
Enter the element to add in list 20
Like to add more elements (y/n) y
Enter the element to add in list 30
Like to add more elements (y/n) n
List is
[10, 20, 30]
10 20 30
Enter element to search 35
element not found
Example:4
Write a Python program to create a list further take input for an element to search, check and print whether the element is present or not using linear search, if present also print its position(using functions)
Sol:
#create an empty list #global blank list n=[] def create_list(): while True: a=int(input("Enter the element to add in list ")) n.append(a) ch=input("Like to add more elements (y/n) ") if(ch=="y" or ch=="Y"): continue else: break def display_list(): # 1st method to display list print("List is ") print(n) # 2nd method to display list for i in n: print(i,end=' ') def search_list(item): i = 0 z = False while(i < len(n)): if(n[i] == item): z = True print("element found at ",i+1," position ") i = i + 1 if(z==False): print("element not found") #function calling create_list() display_list() item=int(input("\n\nEnter element to search ")) search_list(item)
Output:
case 1:
Enter the element to add in list 10
Like to add more elements (y/n) y
Enter the element to add in list 20
Like to add more elements (y/n) y
Enter the element to add in list 10
Like to add more elements (y/n) y
Enter the element to add in list 30
Like to add more elements (y/n) y
Enter the element to add in list 10
Like to add more elements (y/n) n
List is
[10, 20, 10, 30, 10]
10 20 10 30 10
Enter element to search 10
element found at 1 position
element found at 3 position
element found at 5 position
case 2:
Enter the element to add in list 10
Like to add more elements (y/n) y
Enter the element to add in list 20
Like to add more elements (y/n) y
Enter the element to add in list 30
Like to add more elements (y/n) y
Enter the element to add in list 25
Like to add more elements (y/n) n
List is
[10, 20, 30, 25]
10 20 30 25
Enter element to search 28
element not found