From 9b5c97753c5a1ea652cc1355e55be7da477627e0 Mon Sep 17 00:00:00 2001 From: vav7 <121348906+vav7@users.noreply.github.com> Date: Fri, 14 Aug 2026 00:13:07 +0530 Subject: [PATCH 1/3] refactor(searches): add type annotations, docstring, and doctests to double_linear_search_recursion.py --- searches/double_linear_search.py | 66 +++++++++++++++++++------------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/searches/double_linear_search.py b/searches/double_linear_search.py index d9dad3c685b6..236b220372c3 100644 --- a/searches/double_linear_search.py +++ b/searches/double_linear_search.py @@ -1,37 +1,51 @@ -from __future__ import annotations +""" +Recursive Double Linear Search Algorithm +Searches for a target element in a list by recursively checking both ends simultaneously. +""" -def double_linear_search(array: list[int], search_item: int) -> int: + +def double_linear_search_recursion( + sequence: list[int], target: int, start: int = 0, end: int | None = None +) -> int: """ - Iterate through the array from both sides to find the index of search_item. + Recursively searches for target in sequence from both ends. + + Time Complexity: O(n) + Space Complexity: O(n) due to recursive call stack - :param array: the array to be searched - :param search_item: the item to be searched - :return the index of search_item, if search_item is in array, else -1 + :param sequence: A list of integers. + :param target: The integer value to search for. + :param start: Starting index for search window. + :param end: Ending index for search window. + :return: Index of target if found, else -1. - Examples: - >>> double_linear_search([1, 5, 5, 10], 1) + >>> double_linear_search_recursion([1, 2, 3, 4, 5], 1) 0 - >>> double_linear_search([1, 5, 5, 10], 5) - 1 - >>> double_linear_search([1, 5, 5, 10], 100) + >>> double_linear_search_recursion([1, 2, 3, 4, 5], 5) + 4 + >>> double_linear_search_recursion([1, 2, 3, 4, 5], 3) + 2 + >>> double_linear_search_recursion([1, 2, 3, 4, 5], 6) + -1 + >>> double_linear_search_recursion([], 3) -1 - >>> double_linear_search([1, 5, 5, 10], 10) - 3 """ - # define the start and end index of the given array - start_ind, end_ind = 0, len(array) - 1 - while start_ind <= end_ind: - if array[start_ind] == search_item: - return start_ind - elif array[end_ind] == search_item: - return end_ind - else: - start_ind += 1 - end_ind -= 1 - # returns -1 if search_item is not found in array - return -1 + if end is None: + end = len(sequence) - 1 + + if start > end: + return -1 + + if sequence[start] == target: + return start + if sequence[end] == target: + return end + + return double_linear_search_recursion(sequence, target, start + 1, end - 1) if __name__ == "__main__": - print(double_linear_search(list(range(100)), 40)) + import doctest + + doctest.testmod() From 3ef8d4a3be348450928848e7fbe07cc357da3124 Mon Sep 17 00:00:00 2001 From: vav7 <121348906+vav7@users.noreply.github.com> Date: Fri, 14 Aug 2026 00:39:44 +0530 Subject: [PATCH 2/3] Update double_linear_search.py --- searches/double_linear_search.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/searches/double_linear_search.py b/searches/double_linear_search.py index 236b220372c3..ac8258a58b0d 100644 --- a/searches/double_linear_search.py +++ b/searches/double_linear_search.py @@ -1,7 +1,8 @@ """ Recursive Double Linear Search Algorithm -Searches for a target element in a list by recursively checking both ends simultaneously. +Searches for a target element in a list +by recursively checking both ends simultaneously. """ From 13214f1788dd271f1bf60d3a6caeaf145abc1428 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 13 Aug 2026 19:09:58 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- searches/double_linear_search.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/searches/double_linear_search.py b/searches/double_linear_search.py index ac8258a58b0d..bf0f910b6c2f 100644 --- a/searches/double_linear_search.py +++ b/searches/double_linear_search.py @@ -1,7 +1,7 @@ """ Recursive Double Linear Search Algorithm -Searches for a target element in a list +Searches for a target element in a list by recursively checking both ends simultaneously. """