Skip to content

Commit e200aff

Browse files
committed
Add doctests to interpolation_search.py
1 parent 2d8f22a commit e200aff

File tree

1 file changed

+54
-57
lines changed

1 file changed

+54
-57
lines changed

searches/interpolation_search.py

+54-57
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,34 @@
33
"""
44

55

6-
def interpolation_search(sorted_collection, item):
7-
"""Pure implementation of interpolation search algorithm in Python
8-
Be careful collection must be ascending sorted, otherwise result will be
9-
unpredictable
10-
:param sorted_collection: some ascending sorted collection with comparable items
11-
:param item: item value to search
12-
:return: index of found item or None if item is not found
6+
def interpolation_search(sorted_collection: list[int], item: int) -> int | None:
7+
"""
8+
Searches for an item in a sorted collection by interpolation search algorithm.
9+
10+
Args:
11+
sorted_collection: An ascending sorted collection of comparable items.
12+
item : The item value to search for.
13+
14+
Returns:
15+
int: The index of the found item, or None if the item is not found.
16+
Examples:
17+
>>> interpolation_search([1, 2, 3, 4, 5], 2)
18+
1
19+
>>> interpolation_search([1, 2, 3, 4, 5], 4)
20+
3
21+
>>> interpolation_search([1, 2, 3, 4, 5], 6)
22+
>>> interpolation_search([], 1)
23+
>>> interpolation_search([100], 100)
24+
0
25+
>>> interpolation_search([1, 2, 3, 4, 5], 0)
26+
>>> interpolation_search([1, 2, 3, 4, 5], 7)
27+
>>> interpolation_search([1, 2, 3, 4, 5], 2)
28+
1
29+
>>> interpolation_search([1, 2, 3, 4, 5], 0)
30+
>>> interpolation_search([1, 2, 3, 4, 5], 7)
31+
>>> interpolation_search([1, 2, 3, 4, 5], 2)
32+
1
33+
>>> interpolation_search([5, 5, 5, 5, 5], 3)
1334
"""
1435
left = 0
1536
right = len(sorted_collection) - 1
@@ -46,16 +67,35 @@ def interpolation_search(sorted_collection, item):
4667
return None
4768

4869

49-
def interpolation_search_by_recursion(sorted_collection, item, left, right):
70+
def interpolation_search_by_recursion(
71+
sorted_collection: list[int], item: int, left: int = 0, right: int | None = None
72+
) -> int | None:
5073
"""Pure implementation of interpolation search algorithm in Python by recursion
5174
Be careful collection must be ascending sorted, otherwise result will be
5275
unpredictable
5376
First recursion should be started with left=0 and right=(len(sorted_collection)-1)
54-
:param sorted_collection: some ascending sorted collection with comparable items
55-
:param item: item value to search
56-
:return: index of found item or None if item is not found
57-
"""
5877
78+
Args:
79+
sorted_collection: some sorted collection with comparable items
80+
item: item value to search
81+
left: left index in collection
82+
right: right index in collection
83+
84+
Returns:
85+
index of item in collection or None if item is not present
86+
87+
Examples:
88+
>>> interpolation_search_by_recursion([0, 5, 7, 10, 15], 0)
89+
0
90+
>>> interpolation_search_by_recursion([0, 5, 7, 10, 15], 15)
91+
4
92+
>>> interpolation_search_by_recursion([0, 5, 7, 10, 15], 5)
93+
1
94+
>>> interpolation_search_by_recursion([0, 5, 7, 10, 15], 100)
95+
>>> interpolation_search_by_recursion([5, 5, 5, 5, 5], 3)
96+
"""
97+
if right is None:
98+
right = len(sorted_collection) - 1
5999
# avoid divided by 0 during interpolation
60100
if sorted_collection[left] == sorted_collection[right]:
61101
if sorted_collection[left] == item:
@@ -87,50 +127,7 @@ def interpolation_search_by_recursion(sorted_collection, item, left, right):
87127
)
88128

89129

90-
def __assert_sorted(collection):
91-
"""Check if collection is ascending sorted, if not - raises :py:class:`ValueError`
92-
:param collection: collection
93-
:return: True if collection is ascending sorted
94-
:raise: :py:class:`ValueError` if collection is not ascending sorted
95-
Examples:
96-
>>> __assert_sorted([0, 1, 2, 4])
97-
True
98-
>>> __assert_sorted([10, -1, 5])
99-
Traceback (most recent call last):
100-
...
101-
ValueError: Collection must be ascending sorted
102-
"""
103-
if collection != sorted(collection):
104-
raise ValueError("Collection must be ascending sorted")
105-
return True
106-
107-
108130
if __name__ == "__main__":
109-
import sys
131+
import doctest
110132

111-
"""
112-
user_input = input('Enter numbers separated by comma:\n').strip()
113-
collection = [int(item) for item in user_input.split(',')]
114-
try:
115-
__assert_sorted(collection)
116-
except ValueError:
117-
sys.exit('Sequence must be ascending sorted to apply interpolation search')
118-
119-
target_input = input('Enter a single number to be found in the list:\n')
120-
target = int(target_input)
121-
"""
122-
123-
debug = 0
124-
if debug == 1:
125-
collection = [10, 30, 40, 45, 50, 66, 77, 93]
126-
try:
127-
__assert_sorted(collection)
128-
except ValueError:
129-
sys.exit("Sequence must be ascending sorted to apply interpolation search")
130-
target = 67
131-
132-
result = interpolation_search(collection, target)
133-
if result is not None:
134-
print(f"{target} found at positions: {result}")
135-
else:
136-
print("Not found")
133+
doctest.testmod()

0 commit comments

Comments
 (0)