From 1c57978fefaea1bafbf4fabc949080fb4a5e6a11 Mon Sep 17 00:00:00 2001 From: snehapriy958 Date: Mon, 4 May 2026 10:23:19 +0530 Subject: [PATCH 1/5] Fix: improve docstrings and use ValueError in rec_linear_search --- searches/linear_search.py | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/searches/linear_search.py b/searches/linear_search.py index 8adb4a7015f0..6e3775a1375e 100644 --- a/searches/linear_search.py +++ b/searches/linear_search.py @@ -12,18 +12,13 @@ def linear_search(sequence: list, target: int) -> int: """A pure Python implementation of a linear search algorithm - :param sequence: a collection with comparable items (sorting is not required for - linear search) - :param target: item value to search - :return: index of found item or -1 if item is not found + :param sequence: a collection with comparable items (No sorting required) + :param target: Value to search for + :return: index of target if found, else -1 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 """ @@ -35,14 +30,13 @@ def linear_search(sequence: list, target: int) -> int: def rec_linear_search(sequence: list, low: int, high: int, target: int) -> int: """ - A pure Python implementation of a recursive linear search algorithm + Recursive linear search algorithm - :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 + :param sequence: Collection of comparable items (no sorting required) + :param low: Lower index bound + :param high: Higher index bound + :param target: Value to search for + :return: Index of the target if found, else -1 Examples: >>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 0) @@ -55,7 +49,7 @@ def rec_linear_search(sequence: list, low: int, high: int, target: int) -> int: -1 """ if not (0 <= high < len(sequence) and 0 <= low < len(sequence)): - raise Exception("Invalid upper or lower bound!") + raise ValueError("Invalid upper or lower bound!") if high < low: return -1 if sequence[low] == target: From 234f50b0a975ace2fa4bc45affa3209530fa8d41 Mon Sep 17 00:00:00 2001 From: snehapriy958 Date: Mon, 4 May 2026 11:02:48 +0530 Subject: [PATCH 2/5] Improve: handle empty sequence case in linear_search --- searches/linear_search.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/searches/linear_search.py b/searches/linear_search.py index 6e3775a1375e..a4783e4196ea 100644 --- a/searches/linear_search.py +++ b/searches/linear_search.py @@ -7,6 +7,11 @@ For manual testing run: python3 linear_search.py """ +def linear_search(sequence: list, target: int) -> int: + """... existing docstring ...""" + + if not sequence: + return -1 def linear_search(sequence: list, target: int) -> int: From 2ffa363b5e4037b39e48ba2e9dea98bd34238d96 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 4 May 2026 08:35:31 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- searches/linear_search.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/searches/linear_search.py b/searches/linear_search.py index a4783e4196ea..3e0c7f625d51 100644 --- a/searches/linear_search.py +++ b/searches/linear_search.py @@ -7,6 +7,8 @@ For manual testing run: python3 linear_search.py """ + + def linear_search(sequence: list, target: int) -> int: """... existing docstring ...""" From 28fff0ffa368615fa4cc0e7cb5f649de517146c0 Mon Sep 17 00:00:00 2001 From: snehapriy958 Date: Mon, 4 May 2026 14:10:00 +0530 Subject: [PATCH 4/5] Fix: Improve: handle empty sequence case in linear_search --- searches/linear_search.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/searches/linear_search.py b/searches/linear_search.py index 3e0c7f625d51..cb279a0840d5 100644 --- a/searches/linear_search.py +++ b/searches/linear_search.py @@ -8,14 +8,6 @@ python3 linear_search.py """ - -def linear_search(sequence: list, target: int) -> int: - """... existing docstring ...""" - - if not sequence: - return -1 - - def linear_search(sequence: list, target: int) -> int: """A pure Python implementation of a linear search algorithm @@ -29,6 +21,9 @@ def linear_search(sequence: list, target: int) -> int: >>> linear_search([0, 5, 7, 10, 15], 6) -1 """ + if not sequence: + return -1 + for index, item in enumerate(sequence): if item == target: return index From a3425cf9ebd8189bf069dd860677286a71fdd5ad Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 4 May 2026 08:44:22 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- searches/linear_search.py | 1 + 1 file changed, 1 insertion(+) diff --git a/searches/linear_search.py b/searches/linear_search.py index cb279a0840d5..8c0ed0140748 100644 --- a/searches/linear_search.py +++ b/searches/linear_search.py @@ -8,6 +8,7 @@ python3 linear_search.py """ + def linear_search(sequence: list, target: int) -> int: """A pure Python implementation of a linear search algorithm