Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right column | |
|---|---|---|
| 1for i in range(len(items)): | ACompares the current item with the value being searched for | |
| 2if items[i] == target: | BLoops through every index of the list so each item can be checked in turn | |
| 3When the target is found, the search stops and gives back its position (index) | CUses = (assignment) instead of == (comparison), which causes a syntax error. It should be ==. | |
| 4If the loop finishes without finding the target, the search returns -1 to show it is not in the list | Dreturn -1 is inside the loop, so the search gives up after checking only the first item. It should be after the loop, not inside it. | |
| 5if items[i] = target: | Ereturn i | |
| 6for i in range(len(items)): if items[i] == target: return i return -1 | Freturn -1 |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
linear search — An algorithm that uses a loop to check each item in a list in turn. It stops when it finds the target or reaches the end of the list, often returning the index or -1.for i in range(len(items)):
if items[i] == target:
return ifor i in range(len(items)):
if items[i] == target:
return ifor i in range(len(items)):
if items[i] == target: return i
return -1for i in range(len(items)):
if items[i] == target:
breakfor i in range(len(items)):
if items[i] == target:
return i