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 | |
|---|---|---|
| 1mid = (low + high) // 2 | AUsing < instead of <= means the loop stops before checking the last item when low equals high. It should be while low <= high: | |
| 2while low <= high: | Blow never moves past mid, so the loop can get stuck forever on the same range. It should be low = mid + 1 | |
| 3If the middle item is too small, ignore the left half by moving low up past mid | Chigh = mid - 1 | |
| 4If the middle item is too big, ignore the right half by moving high down below mid | DKeeps looping while there is still a range of items left to search | |
| 5while low < high: | EFinds the index of the middle item in the current search range | |
| 6low = mid | Flow = mid + 1 |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
while low <= high: — The binary search loop: while low <= high, check the middle item of a sorted list, then move low or high to halve the search range each time.low = 0
high = len(nums) - 1
while low <= high:high = len(nums) - 1
while low <= high:while low <= high:
mid = (low + high) // 2mid = (low + high) // 2
if nums[mid] < target:
low = mid + 1nums = sorted([9, 2, 7, 4])
low = 0