diff --git a/search.py b/search.py index c3f128c..e660b85 100644 --- a/search.py +++ b/search.py @@ -42,5 +42,19 @@ def binary(data: List[Any], target: Any, comparator: Callable[[Any, Any], int]) - positive if a > b Returns the index of the found element, or -1 if not found. """ - pass + left, right = 0, len(data) - 1 + + while left <= right: + mid = (left + right) // 2 + cmp_result = comparator(data[mid], target) + + if cmp_result == 0: + return mid + elif cmp_result < 0: + left = mid + 1 + else: + right = mid - 1 + + # Raise NotFoundError (required by your test suite) + raise NotFoundError(f"{target} not found")