← Back to Data Structures & Algorithms
Question 526
Linear Search
Linear Search
Write linear_search(values: list[int], target: int) -> int that walks the list from the front with one for loop over range(len(values)) and returns the index of the first element equal to target, or -1 when it is not there. This is O(n): in the worst case every element is checked once. Do NOT write a main block or any top-level test code; the tests construct the calls. Input: two lines, a space-separated list of integers (possibly empty) and then the target, which the tests read and pass to your function before printing the index it returns.
Expected Output:
2= newline. The output ends with a newline — print one after the last line.
Hints3 available
Sign in to use hintsTopics:
Data Structures & Algorithms
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Ctrl+Space to expand shortcuts (pr, inp, def)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.