Write A Function Called Linear_Search_Product That Takes The List Of Products And A Target Product Name As Input The Function Should Perform A Linear Search To Find The Target Product In The List And Return A List Of Indices Of All Occurrences Of The Product If Found Or An Empty List If The Product Is Not Found

Write A Function Called Linear_Search_Product That Takes The List Of Products And A Target Product Name As Input. The Function Should Perform A Linear Search To Find The Target Product In The List And Return A List Of Indices Of All Occurrences Of The Product If Found, Or An Empty List If The Product Is Not Found

 

Hi Guys, welcome to my blog in this article you will learn about the below program

Title : 

Write A Function Called Linear_Search_Product That Takes The List Of Products And A Target Product Name As Input. The Function Should Perform A Linear Search To Find The Target Product In The List And Return A List Of Indices Of All Occurrences Of The Product If Found, Or An Empty List If The Product Is Not Found 

 

Write A Function Called Linear_Search_Product That Takes The List Of Products And A Target Product Name As Input. The Function Should Perform A Linear Search To Find The Target Product In The List And Return A List Of Indices Of All Occurrences Of The Product If Found, Or An Empty List If The Product Is Not Found

 




 

Description : 

Hi guys, in this article you will learn The Python program Uses the Python function, takes arguments as products list and target product and uses Linear search to find the target product among the list of products. The Python function uses the enumerate function to get the index as well as the product from the list of products and checks if the target product is present in the list of products, Then notes down the indices into a list. The Python function then returns the list having the induces where the target product is present.


    

def linear_search_product(products, target_product):
   indices = []
   for index, product in enumerate(products):
       if product.lower() == target_product.lower():
           indices.append(index)
   return indices



    


In the main program you need to take a variable named as length and an empty list named as products and after user to enters all the product present in the list and keep appending list of products Now after you said to enter the target contact and story into another variable. Call the Python function to Linear search the product and take another variable to store the indices returned. Now use the print function called a print all the indexes where all the target product is present in the product.


    

length = int(input("Enter Number of Products : "))
products = []
for _ in range(length):
   temp = input("Enter Product Name : ")
   products.append(temp)

target = input("Enter Target Product Name : ")

indices = linear_search_product(products, target)

print("The Product is Present in Below Indexes")
print(indices)


    




Complete Python Code : 


    

def linear_search_product(products, target_product):
   indices = []
   for index, product in enumerate(products):
       if product.lower() == target_product.lower():
           indices.append(index)
   return indices


length = int(input("Enter Number of Products : "))
products = []
for _ in range(length):
   temp = input("Enter Product Name : ")
   products.append(temp)

target = input("Enter Target Product Name : ")

indices = linear_search_product(products, target)

print("The Product is Present in Below Indexes")
print(indices)


    

 

 

Output : 


    

Enter Number of Products : 5
Enter Product Name : abc
Enter Product Name : def
Enter Product Name : abc
Enter Product Name : xyz
Enter Product Name : 123
Enter Target Product Name : abc
The Product is Present in Below Indexes
[0, 2]


    

 

Output : 


    

Enter Number of Products : 7
Enter Product Name : shampoo
Enter Product Name : biscuit
Enter Product Name : table
Enter Product Name : Table
Enter Product Name : notebook
Enter Product Name : pen
Enter Product Name : table
Enter Target Product Name : table
The Product is Present in Below Indexes
[2, 3, 6]


    



Conclusion :

The above Python program uses the Python function to Linear search the list of products matching with the targeted product and keeps a note of indexes wherever the product appears in the list. The main Python program accepts the list of products from the user, Also accepts the targeted product from the user then calls the function to Linear search the targeted product, to get the indices as return value. The indices are then printed using the wind function call. Comment it down below if you have any query is regarding the above Python program To support me please subscribe to my YouTube channel, so you don't miss out new content https://www.youtube.com/channel/UCUooZAqgfE4ngJ0oXsj3-MA

 

Post a Comment

0 Comments