Skip to content

Commit a8d2f41

Browse files
committed
fix: handle missing lower bound in exponential search
1 parent 6c04620 commit a8d2f41

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

searches/exponential_search.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,19 @@ def exponential_search(sorted_collection: list[int], item: int) -> int:
8181
1
8282
>>> exponential_search([0, 5, 7, 10, 15], 6)
8383
-1
84+
>>> exponential_search([], 1)
85+
-1
86+
>>> exponential_search([1, 1], -1)
87+
-1
8488
"""
8589
if list(sorted_collection) != sorted(sorted_collection):
8690
raise ValueError("sorted_collection must be sorted in ascending order")
8791

92+
if not sorted_collection:
93+
return -1
94+
95+
if sorted_collection[0] > item:
96+
return -1
8897
if sorted_collection[0] == item:
8998
return 0
9099

0 commit comments

Comments
 (0)