3232# Generic comparable type
3333# ---------------------------------------------------------------------------
3434
35- T = TypeVar ("T" ) # Must support < via __lt__; mirrors what bisect itself accepts.
35+ T = TypeVar ("T" ) # Must support < via __lt__; mirrors what bisect itself accepts.
3636
3737
3838# ---------------------------------------------------------------------------
3939# Helpers
4040# ---------------------------------------------------------------------------
4141
42+
4243def _check_sorted (collection : Sequence ) -> None :
4344 """Raise ValueError if *collection* is not sorted in ascending order.
4445
@@ -52,6 +53,7 @@ def _check_sorted(collection: Sequence) -> None:
5253# bisect_left / bisect_right
5354# ---------------------------------------------------------------------------
5455
56+
5557def bisect_left (
5658 sorted_collection : Sequence [T ],
5759 item : T ,
@@ -138,6 +140,7 @@ def bisect_right(
138140# insort helpers
139141# ---------------------------------------------------------------------------
140142
143+
141144def insort_left (
142145 sorted_collection : list [T ],
143146 item : T ,
@@ -198,6 +201,7 @@ def insort_right(
198201# Core binary search variants
199202# ---------------------------------------------------------------------------
200203
204+
201205def binary_search (sorted_collection : Sequence [T ], item : T ) -> int :
202206 """Iterative binary search. Returns -1 when *item* is absent.
203207
@@ -259,9 +263,7 @@ def binary_search_std_lib(sorted_collection: Sequence[T], item: T) -> int:
259263 return - 1
260264
261265
262- def binary_search_with_duplicates (
263- sorted_collection : Sequence [T ], item : T
264- ) -> list [int ]:
266+ def binary_search_with_duplicates (sorted_collection : Sequence [T ], item : T ) -> list [int ]:
265267 """Binary search that returns *all* indices where *item* appears.
266268
267269 IMPROVEMENT: reuses the module-level ``bisect_left`` / ``bisect_right``
@@ -325,9 +327,7 @@ def binary_search_by_recursion(
325327 return _binary_search_recursive (sorted_collection , item , left , right )
326328
327329
328- def _binary_search_recursive (
329- col : Sequence [T ], item : T , left : int , right : int
330- ) -> int :
330+ def _binary_search_recursive (col : Sequence [T ], item : T , left : int , right : int ) -> int :
331331 """Internal recursive helper — no validation overhead."""
332332 if left > right :
333333 return - 1
@@ -344,6 +344,7 @@ def _binary_search_recursive(
344344# Exponential search
345345# ---------------------------------------------------------------------------
346346
347+
347348def exponential_search (sorted_collection : Sequence [T ], item : T ) -> int :
348349 """Exponential search — efficient when the target is near the start.
349350
0 commit comments