GPT-6 Astra × binary-search
10.0Algorithm + full docstring (Args/Returns/Raises + examples)
correctness 10.0quality 10.0documentation 10.016289ms
$ cat binary-search.prompt — what the model was asked
Implement `binary_search(arr: list[int], target: int) -> int` in Python. It should return the index of target in a sorted list, or -1 if not found. Write a proper docstring with Args, Returns, and Raises sections. Add inline comments explaining the algorithm logic. Include 3 test cases as examples in the docstring.
run 1 of 1
10.0correctness 10.0quality 10.0documentation 10.016289ms
# judge: The implementation is correct for empty lists, missing targets, and duplicates, uses clean inclusive bounds and overflow-safe midpoint calculation, and provides a complete docstring with three accurate examples and meaningful comments.
$ cat codex-cli/gpt-6-astra.binary-search.r1.response
```python
def binary_search(arr: list[int], target: int) -> int:
"""Find a target in a list sorted in ascending order.
Args:
arr: A list of integers sorted in nondecreasing order.
target: The integer to find.
Returns:
The index of a matching element, or -1 if target is not found.
If duplicates exist, any matching index may be returned.
Raises:
TypeError: If elements and target cannot be compared. Input types
and sorted order are otherwise assumed, not validated.
Examples:
>>> binary_search([1, 3, 5, 7, 9], 7)
3
>>> binary_search([1, 3, 5, 7, 9], 4)
-1
>>> binary_search([], 5)
-1
"""
# Track the inclusive bounds of the remaining search interval.
left, right = 0, len(arr) - 1
while left <= right:
mid = left + (right - left) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
# Sorted order rules out the midpoint and everything to its left.
left = mid + 1
else:
# Sorted order rules out the midpoint and everything to its right.
right = mid - 1
# An empty search interval means the target is absent.
return -1
```