Skip to content

Commit 19524f3

Browse files
Add exponential_search doctest for empty array edge case (#13954)
* Add exponential_search doctest for empty array edge case This doctest demonstrates that exponential_search correctly handles empty arrays by returning -1. * Improve comments in exponential_search.py Updated comments for clarity and consistency in the exponential search implementation. --------- Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 1672dea commit 19524f3

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

‎searches/exponential_search.py‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#!/usr/bin/env python3
22

33
"""
4-
Pure Python implementation of exponential search algorithm
4+
Pure Python implementation of the exponential search algorithm
55
66
For more information, see the Wikipedia page:
77
https://en.wikipedia.org/wiki/Exponential_search
88
9-
For doctests run the following command:
9+
Run doctests with the following command:
1010
python3 -m doctest -v exponential_search.py
1111
12-
For manual testing run:
12+
For manual testing, run:
1313
python3 exponential_search.py
1414
"""
1515

@@ -22,10 +22,10 @@ def binary_search_by_recursion(
2222
left: int = 0,
2323
right: int | None = None,
2424
) -> int:
25-
"""Pure implementation of binary search algorithm in Python using recursion
25+
"""Pure implementation of the binary search algorithm in Python using recursion
2626
27-
Be careful: the collection must be ascending sorted otherwise, the result will be
28-
unpredictable.
27+
Be careful: the collection must be ascendingly sorted; otherwise, the result will
28+
be unpredictable.
2929
3030
:param sorted_collection: some ascending sorted collection with comparable items
3131
:param item: item value to search
@@ -78,14 +78,14 @@ def exponential_search(sorted_collection: list[int], item: int) -> int:
7878
For more information, refer to:
7979
https://en.wikipedia.org/wiki/Exponential_search
8080
81-
Be careful: the collection must be ascending sorted, otherwise the result will be
82-
unpredictable.
81+
Be careful: the collection must be ascendingly sorted; otherwise the result will
82+
be unpredictable.
8383
8484
:param sorted_collection: some ascending sorted collection with comparable items
8585
:param item: item value to search
8686
:return: index of the found item or -1 if the item is not found
8787
88-
The time complexity of this algorithm is O(log i) where i is the index of the item.
88+
The time complexity of this algorithm is O(log i), where i is the index of the item.
8989
9090
Examples:
9191
>>> exponential_search([0, 5, 7, 10, 15], 0)
@@ -100,6 +100,8 @@ def exponential_search(sorted_collection: list[int], item: int) -> int:
100100
-1
101101
>>> exponential_search([0, 5, 7, 10, 15], 20)
102102
-1
103+
>>> exponential_search([], 1) # Empty array edge case
104+
-1
103105
"""
104106
if list(sorted_collection) != sorted(sorted_collection):
105107
raise ValueError("sorted_collection must be sorted in ascending order")

0 commit comments

Comments
 (0)