Skip to content

Commit f188db1

Browse files
author
deepshekhardas
committed
fix(searches): handle empty list in exponential_search
Fixes IndexError when sorted_collection is empty by returning -1 early before accessing index 0.
1 parent c0db072 commit f188db1

1 file changed

Lines changed: 3 additions & 0 deletions

File tree

searches/exponential_search.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ def exponential_search(sorted_collection: list[int], item: int) -> int:
8585
if list(sorted_collection) != sorted(sorted_collection):
8686
raise ValueError("sorted_collection must be sorted in ascending order")
8787

88+
if not sorted_collection:
89+
return -1
90+
8891
if sorted_collection[0] == item:
8992
return 0
9093

0 commit comments

Comments
 (0)