3
3
"""
4
4
5
5
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: sorted list of integers
12
+ item: item value to search
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) is None
22
+ True
23
+ >>> interpolation_search([], 1) is None
24
+ True
25
+ >>> interpolation_search([100], 100)
26
+ 0
27
+ >>> interpolation_search([1, 2, 3, 4, 5], 0) is None
28
+ True
29
+ >>> interpolation_search([1, 2, 3, 4, 5], 7) is None
30
+ True
31
+ >>> interpolation_search([1, 2, 3, 4, 5], 2)
32
+ 1
33
+ >>> interpolation_search([1, 2, 3, 4, 5], 0) is None
34
+ True
35
+ >>> interpolation_search([1, 2, 3, 4, 5], 7) is None
36
+ True
37
+ >>> interpolation_search([1, 2, 3, 4, 5], 2)
38
+ 1
39
+ >>> interpolation_search([5, 5, 5, 5, 5], 3) is None
40
+ True
13
41
"""
14
42
left = 0
15
43
right = len (sorted_collection ) - 1
@@ -19,8 +47,7 @@ def interpolation_search(sorted_collection, item):
19
47
if sorted_collection [left ] == sorted_collection [right ]:
20
48
if sorted_collection [left ] == item :
21
49
return left
22
- else :
23
- return None
50
+ return None
24
51
25
52
point = left + ((item - sorted_collection [left ]) * (right - left )) // (
26
53
sorted_collection [right ] - sorted_collection [left ]
@@ -33,7 +60,7 @@ def interpolation_search(sorted_collection, item):
33
60
current_item = sorted_collection [point ]
34
61
if current_item == item :
35
62
return point
36
- elif point < left :
63
+ if point < left :
37
64
right = left
38
65
left = point
39
66
elif point > right :
@@ -46,22 +73,42 @@ def interpolation_search(sorted_collection, item):
46
73
return None
47
74
48
75
49
- def interpolation_search_by_recursion (sorted_collection , item , left , right ):
76
+ def interpolation_search_by_recursion (
77
+ sorted_collection : list [int ], item : int , left : int = 0 , right : int | None = None
78
+ ) -> int | None :
50
79
"""Pure implementation of interpolation search algorithm in Python by recursion
51
80
Be careful collection must be ascending sorted, otherwise result will be
52
81
unpredictable
53
82
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
- """
58
83
84
+ Args:
85
+ sorted_collection: some sorted collection with comparable items
86
+ item: item value to search
87
+ left: left index in collection
88
+ right: right index in collection
89
+
90
+ Returns:
91
+ index of item in collection or None if item is not present
92
+
93
+ Examples:
94
+ >>> interpolation_search_by_recursion([0, 5, 7, 10, 15], 0)
95
+ 0
96
+ >>> interpolation_search_by_recursion([0, 5, 7, 10, 15], 15)
97
+ 4
98
+ >>> interpolation_search_by_recursion([0, 5, 7, 10, 15], 5)
99
+ 1
100
+ >>> interpolation_search_by_recursion([0, 5, 7, 10, 15], 100) is None
101
+ True
102
+ >>> interpolation_search_by_recursion([5, 5, 5, 5, 5], 3) is None
103
+ True
104
+ """
105
+ if right is None :
106
+ right = len (sorted_collection ) - 1
59
107
# avoid divided by 0 during interpolation
60
108
if sorted_collection [left ] == sorted_collection [right ]:
61
109
if sorted_collection [left ] == item :
62
110
return left
63
- else :
64
- return None
111
+ return None
65
112
66
113
point = left + ((item - sorted_collection [left ]) * (right - left )) // (
67
114
sorted_collection [right ] - sorted_collection [left ]
@@ -73,64 +120,18 @@ def interpolation_search_by_recursion(sorted_collection, item, left, right):
73
120
74
121
if sorted_collection [point ] == item :
75
122
return point
76
- elif point < left :
123
+ if point < left :
77
124
return interpolation_search_by_recursion (sorted_collection , item , point , left )
78
- elif point > right :
125
+ if point > right :
79
126
return interpolation_search_by_recursion (sorted_collection , item , right , left )
80
- elif sorted_collection [point ] > item :
127
+ if sorted_collection [point ] > item :
81
128
return interpolation_search_by_recursion (
82
129
sorted_collection , item , left , point - 1
83
130
)
84
- else :
85
- return interpolation_search_by_recursion (
86
- sorted_collection , item , point + 1 , right
87
- )
88
-
89
-
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
131
+ return interpolation_search_by_recursion (sorted_collection , item , point + 1 , right )
106
132
107
133
108
134
if __name__ == "__main__" :
109
- import sys
135
+ import doctest
110
136
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" )
137
+ doctest .testmod ()
0 commit comments