2017-10-15 16:11:10 +00:00
|
|
|
"""
|
2020-03-04 12:40:28 +00:00
|
|
|
This is pure Python implementation of interpolation search algorithm
|
2017-10-15 16:11:10 +00:00
|
|
|
"""
|
2017-11-25 09:23:50 +00:00
|
|
|
|
2017-10-15 16:11:10 +00:00
|
|
|
|
|
|
|
def interpolation_search(sorted_collection, item):
|
|
|
|
"""Pure implementation of interpolation search algorithm in Python
|
Interpolation search - fix endless loop bug, divide 0 bug and update description (#793)
* fix endless loop bug, divide 0 bug and update description
fix an endless bug, for example, if collection = [10,30,40,45,50,66,77,93], item = 67.
fix divide 0 bug, when right=left it is not OK to point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left])
update 'sorted' to 'ascending sorted' in description to avoid confusion
* delete swap files
* delete 'address' and add input validation
2019-05-18 02:59:12 +00:00
|
|
|
Be careful collection must be ascending sorted, otherwise result will be
|
2017-10-15 16:11:10 +00:00
|
|
|
unpredictable
|
Interpolation search - fix endless loop bug, divide 0 bug and update description (#793)
* fix endless loop bug, divide 0 bug and update description
fix an endless bug, for example, if collection = [10,30,40,45,50,66,77,93], item = 67.
fix divide 0 bug, when right=left it is not OK to point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left])
update 'sorted' to 'ascending sorted' in description to avoid confusion
* delete swap files
* delete 'address' and add input validation
2019-05-18 02:59:12 +00:00
|
|
|
:param sorted_collection: some ascending sorted collection with comparable items
|
2017-10-15 16:11:10 +00:00
|
|
|
:param item: item value to search
|
|
|
|
:return: index of found item or None if item is not found
|
|
|
|
"""
|
|
|
|
left = 0
|
|
|
|
right = len(sorted_collection) - 1
|
|
|
|
|
|
|
|
while left <= right:
|
2020-01-18 12:24:33 +00:00
|
|
|
# avoid divided by 0 during interpolation
|
2019-10-05 05:14:13 +00:00
|
|
|
if sorted_collection[left] == sorted_collection[right]:
|
|
|
|
if sorted_collection[left] == item:
|
Interpolation search - fix endless loop bug, divide 0 bug and update description (#793)
* fix endless loop bug, divide 0 bug and update description
fix an endless bug, for example, if collection = [10,30,40,45,50,66,77,93], item = 67.
fix divide 0 bug, when right=left it is not OK to point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left])
update 'sorted' to 'ascending sorted' in description to avoid confusion
* delete swap files
* delete 'address' and add input validation
2019-05-18 02:59:12 +00:00
|
|
|
return left
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
point = left + ((item - sorted_collection[left]) * (right - left)) // (
|
|
|
|
sorted_collection[right] - sorted_collection[left]
|
|
|
|
)
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
# out of range check
|
|
|
|
if point < 0 or point >= len(sorted_collection):
|
2017-10-15 16:11:10 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
current_item = sorted_collection[point]
|
|
|
|
if current_item == item:
|
|
|
|
return point
|
|
|
|
else:
|
2019-10-05 05:14:13 +00:00
|
|
|
if point < left:
|
Interpolation search - fix endless loop bug, divide 0 bug and update description (#793)
* fix endless loop bug, divide 0 bug and update description
fix an endless bug, for example, if collection = [10,30,40,45,50,66,77,93], item = 67.
fix divide 0 bug, when right=left it is not OK to point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left])
update 'sorted' to 'ascending sorted' in description to avoid confusion
* delete swap files
* delete 'address' and add input validation
2019-05-18 02:59:12 +00:00
|
|
|
right = left
|
|
|
|
left = point
|
2019-10-05 05:14:13 +00:00
|
|
|
elif point > right:
|
2019-08-19 13:37:49 +00:00
|
|
|
left = right
|
Interpolation search - fix endless loop bug, divide 0 bug and update description (#793)
* fix endless loop bug, divide 0 bug and update description
fix an endless bug, for example, if collection = [10,30,40,45,50,66,77,93], item = 67.
fix divide 0 bug, when right=left it is not OK to point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left])
update 'sorted' to 'ascending sorted' in description to avoid confusion
* delete swap files
* delete 'address' and add input validation
2019-05-18 02:59:12 +00:00
|
|
|
right = point
|
2019-08-19 13:37:49 +00:00
|
|
|
else:
|
Interpolation search - fix endless loop bug, divide 0 bug and update description (#793)
* fix endless loop bug, divide 0 bug and update description
fix an endless bug, for example, if collection = [10,30,40,45,50,66,77,93], item = 67.
fix divide 0 bug, when right=left it is not OK to point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left])
update 'sorted' to 'ascending sorted' in description to avoid confusion
* delete swap files
* delete 'address' and add input validation
2019-05-18 02:59:12 +00:00
|
|
|
if item < current_item:
|
|
|
|
right = point - 1
|
|
|
|
else:
|
|
|
|
left = point + 1
|
2017-10-15 16:11:10 +00:00
|
|
|
return None
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2017-10-15 16:11:10 +00:00
|
|
|
def interpolation_search_by_recursion(sorted_collection, item, left, right):
|
|
|
|
|
|
|
|
"""Pure implementation of interpolation search algorithm in Python by recursion
|
Interpolation search - fix endless loop bug, divide 0 bug and update description (#793)
* fix endless loop bug, divide 0 bug and update description
fix an endless bug, for example, if collection = [10,30,40,45,50,66,77,93], item = 67.
fix divide 0 bug, when right=left it is not OK to point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left])
update 'sorted' to 'ascending sorted' in description to avoid confusion
* delete swap files
* delete 'address' and add input validation
2019-05-18 02:59:12 +00:00
|
|
|
Be careful collection must be ascending sorted, otherwise result will be
|
2017-10-15 16:11:10 +00:00
|
|
|
unpredictable
|
|
|
|
First recursion should be started with left=0 and right=(len(sorted_collection)-1)
|
Interpolation search - fix endless loop bug, divide 0 bug and update description (#793)
* fix endless loop bug, divide 0 bug and update description
fix an endless bug, for example, if collection = [10,30,40,45,50,66,77,93], item = 67.
fix divide 0 bug, when right=left it is not OK to point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left])
update 'sorted' to 'ascending sorted' in description to avoid confusion
* delete swap files
* delete 'address' and add input validation
2019-05-18 02:59:12 +00:00
|
|
|
:param sorted_collection: some ascending sorted collection with comparable items
|
2017-10-15 16:11:10 +00:00
|
|
|
:param item: item value to search
|
|
|
|
:return: index of found item or None if item is not found
|
|
|
|
"""
|
|
|
|
|
2020-01-18 12:24:33 +00:00
|
|
|
# avoid divided by 0 during interpolation
|
2019-10-05 05:14:13 +00:00
|
|
|
if sorted_collection[left] == sorted_collection[right]:
|
|
|
|
if sorted_collection[left] == item:
|
Interpolation search - fix endless loop bug, divide 0 bug and update description (#793)
* fix endless loop bug, divide 0 bug and update description
fix an endless bug, for example, if collection = [10,30,40,45,50,66,77,93], item = 67.
fix divide 0 bug, when right=left it is not OK to point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left])
update 'sorted' to 'ascending sorted' in description to avoid confusion
* delete swap files
* delete 'address' and add input validation
2019-05-18 02:59:12 +00:00
|
|
|
return left
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
point = left + ((item - sorted_collection[left]) * (right - left)) // (
|
|
|
|
sorted_collection[right] - sorted_collection[left]
|
|
|
|
)
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
# out of range check
|
|
|
|
if point < 0 or point >= len(sorted_collection):
|
2017-10-15 16:11:10 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
if sorted_collection[point] == item:
|
|
|
|
return point
|
2019-10-05 05:14:13 +00:00
|
|
|
elif point < left:
|
Interpolation search - fix endless loop bug, divide 0 bug and update description (#793)
* fix endless loop bug, divide 0 bug and update description
fix an endless bug, for example, if collection = [10,30,40,45,50,66,77,93], item = 67.
fix divide 0 bug, when right=left it is not OK to point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left])
update 'sorted' to 'ascending sorted' in description to avoid confusion
* delete swap files
* delete 'address' and add input validation
2019-05-18 02:59:12 +00:00
|
|
|
return interpolation_search_by_recursion(sorted_collection, item, point, left)
|
2019-10-05 05:14:13 +00:00
|
|
|
elif point > right:
|
Interpolation search - fix endless loop bug, divide 0 bug and update description (#793)
* fix endless loop bug, divide 0 bug and update description
fix an endless bug, for example, if collection = [10,30,40,45,50,66,77,93], item = 67.
fix divide 0 bug, when right=left it is not OK to point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left])
update 'sorted' to 'ascending sorted' in description to avoid confusion
* delete swap files
* delete 'address' and add input validation
2019-05-18 02:59:12 +00:00
|
|
|
return interpolation_search_by_recursion(sorted_collection, item, right, left)
|
2017-10-15 16:11:10 +00:00
|
|
|
else:
|
Interpolation search - fix endless loop bug, divide 0 bug and update description (#793)
* fix endless loop bug, divide 0 bug and update description
fix an endless bug, for example, if collection = [10,30,40,45,50,66,77,93], item = 67.
fix divide 0 bug, when right=left it is not OK to point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left])
update 'sorted' to 'ascending sorted' in description to avoid confusion
* delete swap files
* delete 'address' and add input validation
2019-05-18 02:59:12 +00:00
|
|
|
if sorted_collection[point] > item:
|
2019-10-05 05:14:13 +00:00
|
|
|
return interpolation_search_by_recursion(
|
|
|
|
sorted_collection, item, left, point - 1
|
|
|
|
)
|
Interpolation search - fix endless loop bug, divide 0 bug and update description (#793)
* fix endless loop bug, divide 0 bug and update description
fix an endless bug, for example, if collection = [10,30,40,45,50,66,77,93], item = 67.
fix divide 0 bug, when right=left it is not OK to point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left])
update 'sorted' to 'ascending sorted' in description to avoid confusion
* delete swap files
* delete 'address' and add input validation
2019-05-18 02:59:12 +00:00
|
|
|
else:
|
2019-10-05 05:14:13 +00:00
|
|
|
return interpolation_search_by_recursion(
|
|
|
|
sorted_collection, item, point + 1, right
|
|
|
|
)
|
|
|
|
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2017-10-15 16:11:10 +00:00
|
|
|
def __assert_sorted(collection):
|
Interpolation search - fix endless loop bug, divide 0 bug and update description (#793)
* fix endless loop bug, divide 0 bug and update description
fix an endless bug, for example, if collection = [10,30,40,45,50,66,77,93], item = 67.
fix divide 0 bug, when right=left it is not OK to point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left])
update 'sorted' to 'ascending sorted' in description to avoid confusion
* delete swap files
* delete 'address' and add input validation
2019-05-18 02:59:12 +00:00
|
|
|
"""Check if collection is ascending sorted, if not - raises :py:class:`ValueError`
|
2017-10-15 16:11:10 +00:00
|
|
|
:param collection: collection
|
Interpolation search - fix endless loop bug, divide 0 bug and update description (#793)
* fix endless loop bug, divide 0 bug and update description
fix an endless bug, for example, if collection = [10,30,40,45,50,66,77,93], item = 67.
fix divide 0 bug, when right=left it is not OK to point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left])
update 'sorted' to 'ascending sorted' in description to avoid confusion
* delete swap files
* delete 'address' and add input validation
2019-05-18 02:59:12 +00:00
|
|
|
:return: True if collection is ascending sorted
|
|
|
|
:raise: :py:class:`ValueError` if collection is not ascending sorted
|
2017-10-15 16:11:10 +00:00
|
|
|
Examples:
|
|
|
|
>>> __assert_sorted([0, 1, 2, 4])
|
|
|
|
True
|
|
|
|
>>> __assert_sorted([10, -1, 5])
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
Interpolation search - fix endless loop bug, divide 0 bug and update description (#793)
* fix endless loop bug, divide 0 bug and update description
fix an endless bug, for example, if collection = [10,30,40,45,50,66,77,93], item = 67.
fix divide 0 bug, when right=left it is not OK to point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left])
update 'sorted' to 'ascending sorted' in description to avoid confusion
* delete swap files
* delete 'address' and add input validation
2019-05-18 02:59:12 +00:00
|
|
|
ValueError: Collection must be ascending sorted
|
2017-10-15 16:11:10 +00:00
|
|
|
"""
|
|
|
|
if collection != sorted(collection):
|
2019-10-05 05:14:13 +00:00
|
|
|
raise ValueError("Collection must be ascending sorted")
|
2017-10-15 16:11:10 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if __name__ == "__main__":
|
2017-10-15 16:11:10 +00:00
|
|
|
import sys
|
2019-08-19 13:37:49 +00:00
|
|
|
|
Interpolation search - fix endless loop bug, divide 0 bug and update description (#793)
* fix endless loop bug, divide 0 bug and update description
fix an endless bug, for example, if collection = [10,30,40,45,50,66,77,93], item = 67.
fix divide 0 bug, when right=left it is not OK to point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left])
update 'sorted' to 'ascending sorted' in description to avoid confusion
* delete swap files
* delete 'address' and add input validation
2019-05-18 02:59:12 +00:00
|
|
|
"""
|
2019-11-16 07:05:00 +00:00
|
|
|
user_input = input('Enter numbers separated by comma:\n').strip()
|
2017-10-15 16:11:10 +00:00
|
|
|
collection = [int(item) for item in user_input.split(',')]
|
|
|
|
try:
|
|
|
|
__assert_sorted(collection)
|
|
|
|
except ValueError:
|
Interpolation search - fix endless loop bug, divide 0 bug and update description (#793)
* fix endless loop bug, divide 0 bug and update description
fix an endless bug, for example, if collection = [10,30,40,45,50,66,77,93], item = 67.
fix divide 0 bug, when right=left it is not OK to point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left])
update 'sorted' to 'ascending sorted' in description to avoid confusion
* delete swap files
* delete 'address' and add input validation
2019-05-18 02:59:12 +00:00
|
|
|
sys.exit('Sequence must be ascending sorted to apply interpolation search')
|
2017-10-15 16:11:10 +00:00
|
|
|
|
2019-08-19 13:37:49 +00:00
|
|
|
target_input = input('Enter a single number to be found in the list:\n')
|
2017-10-15 16:11:10 +00:00
|
|
|
target = int(target_input)
|
2019-11-16 07:05:00 +00:00
|
|
|
"""
|
Interpolation search - fix endless loop bug, divide 0 bug and update description (#793)
* fix endless loop bug, divide 0 bug and update description
fix an endless bug, for example, if collection = [10,30,40,45,50,66,77,93], item = 67.
fix divide 0 bug, when right=left it is not OK to point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left])
update 'sorted' to 'ascending sorted' in description to avoid confusion
* delete swap files
* delete 'address' and add input validation
2019-05-18 02:59:12 +00:00
|
|
|
|
|
|
|
debug = 0
|
|
|
|
if debug == 1:
|
2019-10-05 05:14:13 +00:00
|
|
|
collection = [10, 30, 40, 45, 50, 66, 77, 93]
|
Interpolation search - fix endless loop bug, divide 0 bug and update description (#793)
* fix endless loop bug, divide 0 bug and update description
fix an endless bug, for example, if collection = [10,30,40,45,50,66,77,93], item = 67.
fix divide 0 bug, when right=left it is not OK to point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left])
update 'sorted' to 'ascending sorted' in description to avoid confusion
* delete swap files
* delete 'address' and add input validation
2019-05-18 02:59:12 +00:00
|
|
|
try:
|
|
|
|
__assert_sorted(collection)
|
|
|
|
except ValueError:
|
2019-10-05 05:14:13 +00:00
|
|
|
sys.exit("Sequence must be ascending sorted to apply interpolation search")
|
Interpolation search - fix endless loop bug, divide 0 bug and update description (#793)
* fix endless loop bug, divide 0 bug and update description
fix an endless bug, for example, if collection = [10,30,40,45,50,66,77,93], item = 67.
fix divide 0 bug, when right=left it is not OK to point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left])
update 'sorted' to 'ascending sorted' in description to avoid confusion
* delete swap files
* delete 'address' and add input validation
2019-05-18 02:59:12 +00:00
|
|
|
target = 67
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2017-10-15 16:11:10 +00:00
|
|
|
result = interpolation_search(collection, target)
|
|
|
|
if result is not None:
|
2019-12-07 05:39:59 +00:00
|
|
|
print(f"{target} found at positions: {result}")
|
2017-10-15 16:11:10 +00:00
|
|
|
else:
|
2019-10-05 05:14:13 +00:00
|
|
|
print("Not found")
|