diff options
| author | Scott <[email protected]> | 2022-01-26 11:24:46 -0800 |
|---|---|---|
| committer | Scott <[email protected]> | 2022-01-26 11:24:46 -0800 |
| commit | 9a2ea7cea8b93a85447bd31e8ea660327469f2df (patch) | |
| tree | cdd44134af30774e8d9318041ddb885396525c7e /list_utils.py | |
| parent | 4c86d98e337703016afc7122cd165bb2498b88bc (diff) | |
Sanity check list is sorted before binary search.
Diffstat (limited to 'list_utils.py')
| -rw-r--r-- | list_utils.py | 11 |
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) |
