Exam-style question
Try this first
Trace this Python linear search for target = 5 and data = [8, 1, 5, 3]. State the values checked in order, the number of comparisons, and the result of the search. for index in range(len(data)): if data[index] == target: print(index) break.
Answer and explanation
Concise answer
The values checked are 8, then 1, then 5.
Why this is correct
At index 0, 8 is not equal to 5. At index 1, 1 is not equal to 5. At index 2, 5 matches the target, so 2 is printed and the break statement ends the search.
Use the concise answer above for a quick check, then reveal the full model answer and detailed explanation.
Common mistake
No common mistake is linked to this question yet.
