3737# Helpers
3838# ---------------------------------------------------------------------------
3939
40+
4041def _check_sorted (collection : Sequence ) -> None :
4142 """Raise ValueError if *collection* is not sorted in ascending order.
4243
@@ -50,6 +51,7 @@ def _check_sorted(collection: Sequence) -> None:
5051# bisect_left / bisect_right
5152# ---------------------------------------------------------------------------
5253
54+
5355def bisect_left (
5456 sorted_collection : Sequence [T ],
5557 item : T ,
@@ -136,6 +138,7 @@ def bisect_right(
136138# insort helpers
137139# ---------------------------------------------------------------------------
138140
141+
139142def insort_left (
140143 sorted_collection : list [T ],
141144 item : T ,
@@ -196,6 +199,7 @@ def insort_right(
196199# Core binary search variants
197200# ---------------------------------------------------------------------------
198201
202+
199203def binary_search (sorted_collection : Sequence [T ], item : T ) -> int :
200204 """Iterative binary search. Returns -1 when *item* is absent.
201205
@@ -257,9 +261,7 @@ def binary_search_std_lib(sorted_collection: Sequence[T], item: T) -> int:
257261 return - 1
258262
259263
260- def binary_search_with_duplicates (
261- sorted_collection : Sequence [T ], item : T
262- ) -> list [int ]:
264+ def binary_search_with_duplicates (sorted_collection : Sequence [T ], item : T ) -> list [int ]:
263265 """Binary search that returns *all* indices where *item* appears.
264266
265267 IMPROVEMENT: reuses the module-level ``bisect_left`` / ``bisect_right``
@@ -323,9 +325,7 @@ def binary_search_by_recursion(
323325 return _binary_search_recursive (sorted_collection , item , left , right )
324326
325327
326- def _binary_search_recursive (
327- col : Sequence [T ], item : T , left : int , right : int
328- ) -> int :
328+ def _binary_search_recursive (col : Sequence [T ], item : T , left : int , right : int ) -> int :
329329 """Internal recursive helper — no validation overhead."""
330330 if left > right :
331331 return - 1
@@ -342,6 +342,7 @@ def _binary_search_recursive(
342342# Exponential search
343343# ---------------------------------------------------------------------------
344344
345+
345346def exponential_search (sorted_collection : Sequence [T ], item : T ) -> int :
346347 """Exponential search — efficient when the target is near the start.
347348
@@ -423,4 +424,4 @@ def exponential_search(sorted_collection: Sequence[T], item: T) -> int:
423424 if result == - 1 :
424425 print (f"{ target } was not found in { collection } ." )
425426 else :
426- print (f"{ target } was found at index { result } of { collection } ." )
427+ print (f"{ target } was found at index { result } of { collection } ." )
0 commit comments