From 3953422383649689ebfa3cba104e48816b80539b Mon Sep 17 00:00:00 2001 From: Ashley Jeji George <40469421+Ashley-J-George@users.noreply.github.com> Date: Mon, 14 Sep 2020 00:16:59 +0530 Subject: [PATCH 1/5] Update linear_search.py Python implementation of recursive linear search algorithm --- searches/linear_search.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/searches/linear_search.py b/searches/linear_search.py index 2056bd7a4916..62bc4ef64915 100644 --- a/searches/linear_search.py +++ b/searches/linear_search.py @@ -38,6 +38,39 @@ def linear_search(sequence, target): return None +def rec_linear_search(sequence, low, high, target) -> int: + ''' + Pure implementation of recursive linear search algorithm in Python + + :param sequence: a collection with comparable items (as sorted items not required + in Linear Search) + :param low: Lower bound of the sequence + :param high: Higher bound of the sequence + :param target: The element to be found + :return: Index of the target or None if target not found + + Examples: + >>> linear_search([0, 5, 7, 10, 15], 0) + 0 + + >>> linear_search([0, 5, 7, 10, 15], 15) + 4 + + >>> linear_search([0, 5, 7, 10, 15], 5) + 1 + + >>> linear_search([0, 5, 7, 10, 15], 6) + + ''' + if high < low: + return None + if sequence[low] == target: + return low + if sequence[high] == target: + return high + return rec_linear_search(sequence, low + 1, high - 1, target) + + if __name__ == "__main__": user_input = input("Enter numbers separated by comma:\n").strip() sequence = [int(item) for item in user_input.split(",")] From d54facc4aa959993044752383a9513ea07b851c1 Mon Sep 17 00:00:00 2001 From: Ashley Jeji George <40469421+Ashley-J-George@users.noreply.github.com> Date: Mon, 14 Sep 2020 13:04:29 +0530 Subject: [PATCH 2/5] Update linear_search.py Added different doctests Added the parameter hints Handled the exception --- searches/linear_search.py | 48 ++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/searches/linear_search.py b/searches/linear_search.py index 62bc4ef64915..41819a4f28e8 100644 --- a/searches/linear_search.py +++ b/searches/linear_search.py @@ -38,37 +38,39 @@ def linear_search(sequence, target): return None -def rec_linear_search(sequence, low, high, target) -> int: +def rec_linear_search(sequence: list, low: int, high: int, target: int) -> int: ''' Pure implementation of recursive linear search algorithm in Python - :param sequence: a collection with comparable items (as sorted items not required - in Linear Search) - :param low: Lower bound of the sequence - :param high: Higher bound of the sequence + :param sequence: An array of items + :param low: Lower bound of the array + :param high: Higher bound of the array :param target: The element to be found - :return: Index of the target or None if target not found + :return: Index of the key or -1 if key not found or None in case of an exception Examples: - >>> linear_search([0, 5, 7, 10, 15], 0) - 0 - - >>> linear_search([0, 5, 7, 10, 15], 15) - 4 - - >>> linear_search([0, 5, 7, 10, 15], 5) - 1 - - >>> linear_search([0, 5, 7, 10, 15], 6) - + >>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 0) + The element 0 is present at index 0 + + >>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 700) + The element 700 is present at index 4 + + >>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 30) + The element 5 is present at index 1 + + >>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, -6) + The element -6 is not present in the array ''' if high < low: - return None - if sequence[low] == target: - return low - if sequence[high] == target: - return high - return rec_linear_search(sequence, low + 1, high - 1, target) + return -1 + try: + if sequence[low] == target: + return low + if sequence[high] == target: + return high + return rec_linear_search(sequence, low + 1, high - 1, target) + except IndexError: + print('Invalid upper or lower bound!') if __name__ == "__main__": From 7b37eb850324565c9495e26f0b0f4989888b425e Mon Sep 17 00:00:00 2001 From: Ashley Jeji George <40469421+Ashley-J-George@users.noreply.github.com> Date: Mon, 14 Sep 2020 13:24:50 +0530 Subject: [PATCH 3/5] Update linear_search.py added parameter hints to linear_search --- searches/linear_search.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/searches/linear_search.py b/searches/linear_search.py index 41819a4f28e8..c047154dd96e 100644 --- a/searches/linear_search.py +++ b/searches/linear_search.py @@ -11,7 +11,7 @@ """ -def linear_search(sequence, target): +def linear_search(sequence: list, target: int) -> int: """Pure implementation of linear search algorithm in Python :param sequence: a collection with comparable items (as sorted items not required From dbf677fd84d3a5c5e71b8c6a56cd59b995edcfc7 Mon Sep 17 00:00:00 2001 From: Ashley Jeji George <40469421+Ashley-J-George@users.noreply.github.com> Date: Mon, 14 Sep 2020 15:31:58 +0530 Subject: [PATCH 4/5] Update linear_search.py Both the functions return the index if the target is found and -1 if it is not found The rec_linear_search raises an exception if there is an indexing problem Made changes in the doc comments --- searches/linear_search.py | 43 ++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/searches/linear_search.py b/searches/linear_search.py index c047154dd96e..5f55b8c6ea62 100644 --- a/searches/linear_search.py +++ b/searches/linear_search.py @@ -30,47 +30,48 @@ def linear_search(sequence: list, target: int) -> int: 1 >>> linear_search([0, 5, 7, 10, 15], 6) - + -1 """ for index, item in enumerate(sequence): if item == target: return index - return None + return -1 def rec_linear_search(sequence: list, low: int, high: int, target: int) -> int: - ''' + """ Pure implementation of recursive linear search algorithm in Python - :param sequence: An array of items + :param sequence: a collection with comparable items (as sorted items not required + in Linear Search) :param low: Lower bound of the array :param high: Higher bound of the array :param target: The element to be found - :return: Index of the key or -1 if key not found or None in case of an exception + :return: Index of the key or -1 if key not found Examples: >>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 0) - The element 0 is present at index 0 - + 0 + >>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 700) - The element 700 is present at index 4 - + 4 + >>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 30) - The element 5 is present at index 1 - + 1 + >>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, -6) - The element -6 is not present in the array - ''' + -1 + """ + if (0 > high or high >= len(sequence)) or (0 > low or low >= len(sequence)): + raise Exception("Invalid upper or lower bound!") + if high < low: return -1 - try: - if sequence[low] == target: - return low - if sequence[high] == target: - return high - return rec_linear_search(sequence, low + 1, high - 1, target) - except IndexError: - print('Invalid upper or lower bound!') + if sequence[low] == target: + return low + if sequence[high] == target: + return high + return rec_linear_search(sequence, low + 1, high - 1, target) if __name__ == "__main__": From 852069491a12bd148ad35c0a87c4f6548f5ee585 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 14 Sep 2020 12:37:55 +0200 Subject: [PATCH 5/5] Update linear_search.py --- searches/linear_search.py | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/searches/linear_search.py b/searches/linear_search.py index 5f55b8c6ea62..777080d14e36 100644 --- a/searches/linear_search.py +++ b/searches/linear_search.py @@ -2,17 +2,15 @@ This is pure Python implementation of linear search algorithm For doctests run following command: -python -m doctest -v linear_search.py -or python3 -m doctest -v linear_search.py For manual testing run: -python linear_search.py +python3 linear_search.py """ def linear_search(sequence: list, target: int) -> int: - """Pure implementation of linear search algorithm in Python + """A pure Python implementation of a linear search algorithm :param sequence: a collection with comparable items (as sorted items not required in Linear Search) @@ -22,13 +20,10 @@ def linear_search(sequence: list, target: int) -> int: Examples: >>> linear_search([0, 5, 7, 10, 15], 0) 0 - >>> linear_search([0, 5, 7, 10, 15], 15) 4 - >>> linear_search([0, 5, 7, 10, 15], 5) 1 - >>> linear_search([0, 5, 7, 10, 15], 6) -1 """ @@ -40,7 +35,7 @@ def linear_search(sequence: list, target: int) -> int: def rec_linear_search(sequence: list, low: int, high: int, target: int) -> int: """ - Pure implementation of recursive linear search algorithm in Python + A pure Python implementation of a recursive linear search algorithm :param sequence: a collection with comparable items (as sorted items not required in Linear Search) @@ -52,19 +47,15 @@ def rec_linear_search(sequence: list, low: int, high: int, target: int) -> int: Examples: >>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 0) 0 - >>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 700) 4 - >>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 30) 1 - >>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, -6) -1 """ - if (0 > high or high >= len(sequence)) or (0 > low or low >= len(sequence)): + if not (0 <= high < len(sequence) and 0 <= low < len(sequence)): raise Exception("Invalid upper or lower bound!") - if high < low: return -1 if sequence[low] == target: @@ -76,12 +67,11 @@ def rec_linear_search(sequence: list, low: int, high: int, target: int) -> int: if __name__ == "__main__": user_input = input("Enter numbers separated by comma:\n").strip() - sequence = [int(item) for item in user_input.split(",")] + sequence = [int(item.strip()) for item in user_input.split(",")] - target_input = input("Enter a single number to be found in the list:\n") - target = int(target_input) + target = int(input("Enter a single number to be found in the list:\n").strip()) result = linear_search(sequence, target) - if result is not None: - print(f"{target} found at position : {result}") + if result != -1: + print(f"linear_search({sequence}, {target}) = {result}") else: - print("Not found") + print(f"{target} was not found in {sequence}")