From e200aff72991518172270a6e981b55369507b78d Mon Sep 17 00:00:00 2001 From: Ihor Pryyma Date: Thu, 18 Jul 2024 13:29:09 +0300 Subject: [PATCH 1/5] Add doctests to interpolation_search.py --- searches/interpolation_search.py | 111 +++++++++++++++---------------- 1 file changed, 54 insertions(+), 57 deletions(-) diff --git a/searches/interpolation_search.py b/searches/interpolation_search.py index 0591788aa40b..44883d6bd103 100644 --- a/searches/interpolation_search.py +++ b/searches/interpolation_search.py @@ -3,13 +3,34 @@ """ -def interpolation_search(sorted_collection, item): - """Pure implementation of interpolation search algorithm in Python - Be careful collection must be ascending sorted, otherwise result will be - unpredictable - :param sorted_collection: some ascending sorted collection with comparable items - :param item: item value to search - :return: index of found item or None if item is not found +def interpolation_search(sorted_collection: list[int], item: int) -> int | None: + """ + Searches for an item in a sorted collection by interpolation search algorithm. + + Args: + sorted_collection: An ascending sorted collection of comparable items. + item : The item value to search for. + + Returns: + int: The index of the found item, or None if the item is not found. + Examples: + >>> interpolation_search([1, 2, 3, 4, 5], 2) + 1 + >>> interpolation_search([1, 2, 3, 4, 5], 4) + 3 + >>> interpolation_search([1, 2, 3, 4, 5], 6) + >>> interpolation_search([], 1) + >>> interpolation_search([100], 100) + 0 + >>> interpolation_search([1, 2, 3, 4, 5], 0) + >>> interpolation_search([1, 2, 3, 4, 5], 7) + >>> interpolation_search([1, 2, 3, 4, 5], 2) + 1 + >>> interpolation_search([1, 2, 3, 4, 5], 0) + >>> interpolation_search([1, 2, 3, 4, 5], 7) + >>> interpolation_search([1, 2, 3, 4, 5], 2) + 1 + >>> interpolation_search([5, 5, 5, 5, 5], 3) """ left = 0 right = len(sorted_collection) - 1 @@ -46,16 +67,35 @@ def interpolation_search(sorted_collection, item): return None -def interpolation_search_by_recursion(sorted_collection, item, left, right): +def interpolation_search_by_recursion( + sorted_collection: list[int], item: int, left: int = 0, right: int | None = None +) -> int | None: """Pure implementation of interpolation search algorithm in Python by recursion Be careful collection must be ascending sorted, otherwise result will be unpredictable First recursion should be started with left=0 and right=(len(sorted_collection)-1) - :param sorted_collection: some ascending sorted collection with comparable items - :param item: item value to search - :return: index of found item or None if item is not found - """ + Args: + sorted_collection: some sorted collection with comparable items + item: item value to search + left: left index in collection + right: right index in collection + + Returns: + index of item in collection or None if item is not present + + Examples: + >>> interpolation_search_by_recursion([0, 5, 7, 10, 15], 0) + 0 + >>> interpolation_search_by_recursion([0, 5, 7, 10, 15], 15) + 4 + >>> interpolation_search_by_recursion([0, 5, 7, 10, 15], 5) + 1 + >>> interpolation_search_by_recursion([0, 5, 7, 10, 15], 100) + >>> interpolation_search_by_recursion([5, 5, 5, 5, 5], 3) + """ + if right is None: + right = len(sorted_collection) - 1 # avoid divided by 0 during interpolation if sorted_collection[left] == sorted_collection[right]: if sorted_collection[left] == item: @@ -87,50 +127,7 @@ def interpolation_search_by_recursion(sorted_collection, item, left, right): ) -def __assert_sorted(collection): - """Check if collection is ascending sorted, if not - raises :py:class:`ValueError` - :param collection: collection - :return: True if collection is ascending sorted - :raise: :py:class:`ValueError` if collection is not ascending sorted - Examples: - >>> __assert_sorted([0, 1, 2, 4]) - True - >>> __assert_sorted([10, -1, 5]) - Traceback (most recent call last): - ... - ValueError: Collection must be ascending sorted - """ - if collection != sorted(collection): - raise ValueError("Collection must be ascending sorted") - return True - - if __name__ == "__main__": - import sys + import doctest - """ - user_input = input('Enter numbers separated by comma:\n').strip() - collection = [int(item) for item in user_input.split(',')] - try: - __assert_sorted(collection) - except ValueError: - sys.exit('Sequence must be ascending sorted to apply interpolation search') - - target_input = input('Enter a single number to be found in the list:\n') - target = int(target_input) - """ - - debug = 0 - if debug == 1: - collection = [10, 30, 40, 45, 50, 66, 77, 93] - try: - __assert_sorted(collection) - except ValueError: - sys.exit("Sequence must be ascending sorted to apply interpolation search") - target = 67 - - result = interpolation_search(collection, target) - if result is not None: - print(f"{target} found at positions: {result}") - else: - print("Not found") + doctest.testmod() From b47e45e6b76c538936869ffe246dee4e300bb00d Mon Sep 17 00:00:00 2001 From: Ihor Pryyma Date: Sat, 20 Jul 2024 10:20:32 +0300 Subject: [PATCH 2/5] update docs --- searches/interpolation_search.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/searches/interpolation_search.py b/searches/interpolation_search.py index 44883d6bd103..229b94c77734 100644 --- a/searches/interpolation_search.py +++ b/searches/interpolation_search.py @@ -8,8 +8,8 @@ def interpolation_search(sorted_collection: list[int], item: int) -> int | None: Searches for an item in a sorted collection by interpolation search algorithm. Args: - sorted_collection: An ascending sorted collection of comparable items. - item : The item value to search for. + sorted_collection: sorted list of integers + item: item value to search Returns: int: The index of the found item, or None if the item is not found. From bbe93ad340fc35dd76fc469d328ac2b820a5e5c4 Mon Sep 17 00:00:00 2001 From: Ihor Pryyma Date: Thu, 25 Jul 2024 17:06:42 +0300 Subject: [PATCH 3/5] update tests --- searches/interpolation_search.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/searches/interpolation_search.py b/searches/interpolation_search.py index 229b94c77734..954d8993c662 100644 --- a/searches/interpolation_search.py +++ b/searches/interpolation_search.py @@ -18,19 +18,19 @@ def interpolation_search(sorted_collection: list[int], item: int) -> int | None: 1 >>> interpolation_search([1, 2, 3, 4, 5], 4) 3 - >>> interpolation_search([1, 2, 3, 4, 5], 6) - >>> interpolation_search([], 1) + >>> interpolation_search([1, 2, 3, 4, 5], 6) is None + >>> interpolation_search([], 1) is None >>> interpolation_search([100], 100) 0 - >>> interpolation_search([1, 2, 3, 4, 5], 0) - >>> interpolation_search([1, 2, 3, 4, 5], 7) + >>> interpolation_search([1, 2, 3, 4, 5], 0) is None + >>> interpolation_search([1, 2, 3, 4, 5], 7) is None >>> interpolation_search([1, 2, 3, 4, 5], 2) 1 - >>> interpolation_search([1, 2, 3, 4, 5], 0) - >>> interpolation_search([1, 2, 3, 4, 5], 7) + >>> interpolation_search([1, 2, 3, 4, 5], 0) is None + >>> interpolation_search([1, 2, 3, 4, 5], 7) is None >>> interpolation_search([1, 2, 3, 4, 5], 2) 1 - >>> interpolation_search([5, 5, 5, 5, 5], 3) + >>> interpolation_search([5, 5, 5, 5, 5], 3) is None """ left = 0 right = len(sorted_collection) - 1 From 9a497b07d37a7b8c40a35e36fe49738c228d0510 Mon Sep 17 00:00:00 2001 From: Ihor Pryyma Date: Thu, 25 Jul 2024 17:17:24 +0300 Subject: [PATCH 4/5] update tests 2 --- searches/interpolation_search.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/searches/interpolation_search.py b/searches/interpolation_search.py index 954d8993c662..1c8cbe075ea8 100644 --- a/searches/interpolation_search.py +++ b/searches/interpolation_search.py @@ -19,18 +19,25 @@ def interpolation_search(sorted_collection: list[int], item: int) -> int | None: >>> interpolation_search([1, 2, 3, 4, 5], 4) 3 >>> interpolation_search([1, 2, 3, 4, 5], 6) is None + True >>> interpolation_search([], 1) is None + True >>> interpolation_search([100], 100) 0 >>> interpolation_search([1, 2, 3, 4, 5], 0) is None + True >>> interpolation_search([1, 2, 3, 4, 5], 7) is None + True >>> interpolation_search([1, 2, 3, 4, 5], 2) 1 >>> interpolation_search([1, 2, 3, 4, 5], 0) is None + True >>> interpolation_search([1, 2, 3, 4, 5], 7) is None + True >>> interpolation_search([1, 2, 3, 4, 5], 2) 1 >>> interpolation_search([5, 5, 5, 5, 5], 3) is None + True """ left = 0 right = len(sorted_collection) - 1 @@ -91,8 +98,10 @@ def interpolation_search_by_recursion( 4 >>> interpolation_search_by_recursion([0, 5, 7, 10, 15], 5) 1 - >>> interpolation_search_by_recursion([0, 5, 7, 10, 15], 100) - >>> interpolation_search_by_recursion([5, 5, 5, 5, 5], 3) + >>> interpolation_search_by_recursion([0, 5, 7, 10, 15], 100) is None + True + >>> interpolation_search_by_recursion([5, 5, 5, 5, 5], 3) is None + True """ if right is None: right = len(sorted_collection) - 1 @@ -131,3 +140,4 @@ def interpolation_search_by_recursion( import doctest doctest.testmod() + x = 10 From 955af042723f648c2378e6c3c2bbd849e8ff06ae Mon Sep 17 00:00:00 2001 From: Ihor Pryyma Date: Thu, 25 Jul 2024 17:26:31 +0300 Subject: [PATCH 5/5] clean code --- searches/interpolation_search.py | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/searches/interpolation_search.py b/searches/interpolation_search.py index 1c8cbe075ea8..cb3e0011d0da 100644 --- a/searches/interpolation_search.py +++ b/searches/interpolation_search.py @@ -47,8 +47,7 @@ def interpolation_search(sorted_collection: list[int], item: int) -> int | None: if sorted_collection[left] == sorted_collection[right]: if sorted_collection[left] == item: return left - else: - return None + return None point = left + ((item - sorted_collection[left]) * (right - left)) // ( sorted_collection[right] - sorted_collection[left] @@ -61,7 +60,7 @@ def interpolation_search(sorted_collection: list[int], item: int) -> int | None: current_item = sorted_collection[point] if current_item == item: return point - elif point < left: + if point < left: right = left left = point elif point > right: @@ -109,8 +108,7 @@ def interpolation_search_by_recursion( if sorted_collection[left] == sorted_collection[right]: if sorted_collection[left] == item: return left - else: - return None + return None point = left + ((item - sorted_collection[left]) * (right - left)) // ( sorted_collection[right] - sorted_collection[left] @@ -122,22 +120,18 @@ def interpolation_search_by_recursion( if sorted_collection[point] == item: return point - elif point < left: + if point < left: return interpolation_search_by_recursion(sorted_collection, item, point, left) - elif point > right: + if point > right: return interpolation_search_by_recursion(sorted_collection, item, right, left) - elif sorted_collection[point] > item: + if sorted_collection[point] > item: return interpolation_search_by_recursion( sorted_collection, item, left, point - 1 ) - else: - return interpolation_search_by_recursion( - sorted_collection, item, point + 1, right - ) + return interpolation_search_by_recursion(sorted_collection, item, point + 1, right) if __name__ == "__main__": import doctest doctest.testmod() - x = 10