Python: Write a program that performs a sequential search on an ordered list of 1000 elements. The search on the list should be on a randomly generated number between 1-1000.
Please find below code and don't forget to give a Like.
Please refer below screenshot for indentation and output.
Code:
import random def sequential_search(): #generating random number in range 1 to 1000 to check in list random_num=random.randrange(1,1000) #taking the ordered_list from 1 to 1000 as 1000 elements should be present ordered_list=[*range(1,1001,1)] a=False print("Number we are searching in the order list of (1-1000) is",random_num) for i in range(len(ordered_list)): if(random_num==ordered_list[i]): a=True print("Number ",random_num," is found at the position",i) if(a==False): #if the element is not found then a will be false print("Number ",random_num," is not found in the list") sequential_search()
Output:
Get Answers For Free
Most questions answered within 1 hours.