Answered step by step
Verified Expert Solution
Link Copied!

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

image text in transcribed

Code is in Python:

image text in transcribed

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.") image text in transcribed

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 found

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Basics Computer EngineeringInformation Warehouse Basics From Science

Authors: Odiljon Jakbarov ,Anvarkhan Majidov

1st Edition

620675183X, 978-6206751830

More Books

Students also viewed these Databases questions

Question

List the different categories of international employees. page 642

Answered: 1 week ago

Question

Explain the legal environments impact on labor relations. page 590

Answered: 1 week ago