Question
I am having trouble figuring out this problem, and I would very much appreciate any help. Thank you in advance. ///Q. Modify the linearSearch function
I am having trouble figuring out this problem, and I would very much appreciate any help. Thank you in advance.
///Q. Modify the linearSearch function so that the search of a sorted list will halt when the target is less than a given element in a list. The program will return the position of the search item in the list or a -1 if the item is not in the list. Use the following lists to test your program:
Target 7 list =[0,1,2,3,4,5,6,7,8,9]
Target 7 list = [0,1,2,3,4,5,6]
Target 3 list = [0,1,2,3,4,5,6,8,9]
In the program documentation, state the computational complexity, using big-O notation, of its best, worst, and average case performances.///
This is what I have so far and I can change it so that it reads the correct element in the list, but then it will find numbers that are not in the list. If I remember correctly the verbal instructions said that I would need an elif statement and a break statement, but everything I try is not working correctly. Thank you very much for any help and here is the code I am working with.
def main():
#Returns the position of the target item if found, or -1 otherwise. def linearSearch(target, lyst): position = 0 while position < len(lyst): if target == lyst[position]: return position position += 1 return -1
linList1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] linList2 = [0, 1, 2, 3, 4, 5, 6] linList3 = [0, 1, 2, 3, 4, 5, 6, 8, 9]
numInput = int(input("What target number are you looking for? ")) numFound = linearSearch(numInput, linList3) if numFound: print("This element is at index:", numFound) main()
Step 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