summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--list_utils.py11
1 files changed, 11 insertions, 0 deletions
diff --git a/list_utils.py b/list_utils.py
index 88c436b..1b27436 100644
--- a/list_utils.py
+++ b/list_utils.py
@@ -248,7 +248,18 @@ def binary_search(lst: Sequence[Any], target: Any) -> Tuple[bool, int]:
>>> binary_search(a, 2)
(False, 1)
+ >>> a.append(9)
+ >>> binary_search(a, 4)
+ Traceback (most recent call last):
+ ...
+ AssertionError
+
"""
+ last = None
+ for x in lst:
+ if last is not None:
+ assert x >= last # This asserts iff the list isn't sorted
+ last = x # in ascending order.
return _binary_search(lst, target, 0, len(lst) - 1)