A sorted sequence lets you rule out many positions with one comparison. Binary search maintains a candidate interval, inspects its middle value, and searches only the half that could still contain the target. This lesson teaches the search itself; you do not need a later search module to implement it.
Use an ascending, nondecreasing list of integers. If its middle value is smaller than the target, every value to the left is also too small. If the middle value is larger, every value to the right is too large. Equality ends the search. An unsorted input does not justify either conclusion, so sorted input is a precondition, not something the function fixes by sorting.
Keep half-open bounds [low, high): low is included and high is excluded. Start at zero and the list length. An interval is empty when low is at least high. Check that base case before reading the middle. Otherwise choose (low + high) // 2, then recurse into [mid + 1, high) or [low, mid). Excluding mid after a miss guarantees progress, including for a one-element interval.
catalog = [3, 8, 12, 19, 27]
low, high = 0, len(catalog)
middle = (low + high) // 2
print(middle, catalog[middle]) # 2 12
Searching this example for 19 discards the middle and everything before it. You still use original-list indices; slicing would create a new list and make indices harder to interpret.
Discarding the first midpoint moves the included left boundary to index 4 while the excluded right boundary stays at 7.
Write a fresh function binary_search_recursive(values, target) returning an index whose value equals the target, or None if absent. With duplicates, any matching index is valid. A nested helper can keep the list and target in scope while receiving only low and high. Do not sort, scan, use index, or slice: implement the recursive halving algorithm.
At most one half is searched per call, so comparisons and stack space are O(log n), with constant work for empty input. This assumes bounded-cost integer comparisons and list indexing. Input sortedness is not checked inside the function because a full validation scan would add O(n) work. The caller must provide sorted data, and the function leaves that data unchanged.
The helper, midpoint calculation, comparisons, and initial bounds are supplied. Complete the empty case and equality case before the two directional recursive returns. Each direction has its own check; the final check also covers misses and duplicates.
None when the candidate interval is empty.mid when its value equals the target.[low, mid) when the midpoint is too large.[mid + 1, high) when the midpoint is too small.Edit main.py, then press Run to inspect the supplied probe. Press Submit to check the operation on additional inputs. You do not need to copy code from another lesson.