mager-bench

Gemini 2.5 Flash × binary-search

8.3

Algorithm + full docstring (Args/Returns/Raises + examples)

correctness 9.0quality 8.0documentation 8.06290ms
$ 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

8.3
correctness 9.0quality 8.0documentation 8.06290ms

# judge: Algorithm is correct and handles edge cases well, but Raises section is empty (task requested a Raises section, ideally documenting behavior like non-sorted input or type errors), and comments are somewhat verbose/restating code in places.

$ cat gemini-2.5-flash.binary-search.r1.response
```python
def binary_search(arr: list[int], target: int) -> int:
    """
    Searches for a target element in a sorted list using the binary search algorithm.

    Args:
        arr: A sorted list of integers.
        target: The integer value to search for.

    Returns:
        The index of the target in the list if found, otherwise -1.

    Raises:
        None

    Examples:
        >>> binary_search([1, 3, 5, 7, 9, 11, 13], 7)
        3
        >>> binary_search([10, 20, 30, 40, 50], 15)
        -1
        >>> binary_search([-5, 0, 5, 10, 15], -5)
        0
    """
    # Initialize the low pointer to the beginning of the array.
    low = 0
    # Initialize the high pointer to the end of the array.
    high = len(arr) - 1

    # Continue searching as long as the low pointer is less than or equal to the high pointer.
    while low <= high:
        # Calculate the middle index.
        # Using low + (high - low) // 2 prevents potential integer overflow
        # that could occur with (low + high) // 2 if low and high are very large,
        # though this is less common in Python due to arbitrary-precision integers.
        mid = low + (high - low) // 2

        # Check if the element at the middle index is the target.
        if arr[mid] == target:
            # If it is, we found the target, return its index.
            return mid
        # If the element at the middle index is less than the target,
        # it means the target must be in the right half of the current search range.
        elif arr[mid] < target:
            # Move the low pointer to mid + 1 to search in the right half.
            low = mid + 1
        # If the element at the middle index is greater than the target,
        # it means the target must be in the left half of the current search range.
        else:  # arr[mid] > target
            # Move the high pointer to mid - 1 to search in the left half.
            high = mid - 1

    # If the loop finishes, it means low > high, and the target was not found in the array.
    return -1

if __name__ == '__main__':
    import doctest
    doctest.testmod()

    # Manual test cases for verification
    print(f"Test 1: [1, 3, 5, 7, 9, 11, 13], target=7 -> Expected: 3, Got: {binary_search([1, 3, 5, 7, 9, 11, 13], 7)}")
    print(f"Test 2: [10, 20, 30, 40, 50], target=15 -> Expected: -1, Got: {binary_search([10, 20, 30, 40, 50], 15)}")
    print(f"Test 3: [-5, 0, 5, 10, 15], target=-5 -> Expected: 0, Got: {binary_search([-5, 0, 5, 10, 15], -5)}")
    print(f"Test 4: [2, 4, 6, 8, 10], target=10 -> Expected: 4, Got: {binary_search([2, 4, 6, 8, 10], 10)}")
    print(f"Test 5: [2, 4, 6, 8, 10], target=2 -> Expected: 0, Got: {binary_search([2, 4, 6, 8, 10], 2)}")
    print(f"Test 6: [], target=5 -> Expected: -1, Got: {binary_search([], 5)}")
    print(f"Test 7: [5], target=5 -> Expected: 0, Got: {binary_search([5], 5)}")
    print(f"Test 8: [5], target=10 -> Expected: -1, Got: {binary_search([5], 10)}")
```