Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Code is in Python: def LinearSearch(needle,haystack): return False #Do not make changes below this line. import random print(Welcome to Linear Search Test) x=input(Enter Seed to
Code is in Python:
def LinearSearch(needle,haystack):
return False
#Do not make changes below this line. import random print("Welcome to Linear Search Test") x=input("Enter Seed to Test: ") random.seed(x) target=random.randint(0,20) list=[random.randint(0,20) for x in range(0,10)] list.sort() print("Looking for",target) print("In list",list) x = LinearSearch(target,list) if x==True: print("Target was found.") else: print("Target was not found.")
2.4 Linear Search Complete the Python3 implementation of the Linear Search Algorithm given below. Each time you compare the needle to an element in the haystack, print out what you are comparing. Here is an example execution Welcome to Linear Search Test Enter Seed to Test: 12 Looking for 18 In list [0, 3, 4, 5, 9, 11, 13, 16, 18, 201 Comparing element o to needle 18 Comparing element 3 to needle 18 Comparing element 4 to needle 18 Comparing element 5 to needle 18 Comparing element 9 to needle 18 Comparing element 11 to needle 18 Comparing element 13 to needle 18 Comparing element 16 to needle 18 Comparing element 18 to needle 18 Target was found. The Pseudocode from lecture is given below as a guide. function LinearSearch (needle, haystack) for x=0; x needle then return false end if end for return false end function def LinearSearch(needle, haystack): return false #Do not make changes below this line. import random print("Welcome to Linear Search Test") x=input("Enter Seed to Test: ") random.seed(x) target=random.randint(0,20) list=[random.randint(0,20) for x in range(0,10)] list.sort() print("Looking for", target) print("In list", list) X = LinearSearch(target, list) if x==True: print("Target was found.") else: print("Target was not found.") Input 12 Your output Welcome to Linear Search Test Enter Seed to Test: Looking for 18 In list [0, 3, 4, 5, 9, 11, 13, 16, 18, 20] Target was not found. Expected output Welcome to Linear Search Test Enter Seed to Test: Looking for 18 In list [0, 3, 4, 5, 9, 11, 13, 16, 18, 20] Comparing element 0 to needle 184 Comparing element 3 to needle 184 Comparing element 4 to needle 184 Comparing element 5 to needle 184 Comparing element 9 to needle 184 Comparing element 11 to needle 18+ Comparing element 13 to needle 18+ Comparing element 16 to needle 18+ Comparing element 18 to needle 184 Target was foundStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started