From f08ead8629a63f01e01c2b15f4db2e59071c6f1d Mon Sep 17 00:00:00 2001 From: Reinhold <63191266+reinhold-b@users.noreply.github.com> Date: Thu, 2 Jul 2020 14:59:25 +0200 Subject: [PATCH 01/11] linear search iterating from both array sides --- searches/double_linear_search.py | 47 ++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 searches/double_linear_search.py diff --git a/searches/double_linear_search.py b/searches/double_linear_search.py new file mode 100644 index 000000000000..9dfb440b7581 --- /dev/null +++ b/searches/double_linear_search.py @@ -0,0 +1,47 @@ +"""Implementation of a double-linear-search, +which iterates through the array from both sides: start and end. + +Change ARRAY_LENGTH to a number you want to generate an example array, +which goes up to the number you define: +ARRAY_LENGTH = 50 --> [0, 1, 2, ..., 49, 50] +""" + +# change for bigger/smaller arrays +ARRAY_LENGTH = 100 + + +def double_linear_search(array, x): + """:param array: the array to be searched + :param x: the value to be searched (float, int or string) + :returns index of x, if x is in array, else -1 """ + + # 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] == x: + return start_ind + elif array[end_ind] == x: + return end_ind + + else: + # increase start_ind, decrease end_ind + start_ind += 1 + end_ind -= 1 + + # returns -1 if the element x is not found in the given array + return -1 + + +if __name__ == "__main__": + # should be at index 40 + x = 100 + + # this creates an array, which goes up to the number + # ARRAY_LENGTH --> [0, 1, ... , 99, 100] + array = [x for x in range(ARRAY_LENGTH + 1)] + + # print(array) + + print(double_linear_search(array, x)) From 27a93e69568ef2a560444fa903bd3485850957c9 Mon Sep 17 00:00:00 2001 From: Reinhold <63191266+reinhold-b@users.noreply.github.com> Date: Thu, 2 Jul 2020 15:00:44 +0200 Subject: [PATCH 02/11] Update double_linear_search.py --- 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 9dfb440b7581..05c20d3fc7ea 100644 --- a/searches/double_linear_search.py +++ b/searches/double_linear_search.py @@ -36,7 +36,7 @@ def double_linear_search(array, x): if __name__ == "__main__": # should be at index 40 - x = 100 + x = 40 # this creates an array, which goes up to the number # ARRAY_LENGTH --> [0, 1, ... , 99, 100] From e758d17d2d0c7cda4dcd666bb5922825baebcd1a Mon Sep 17 00:00:00 2001 From: Reinhold <63191266+reinhold-b@users.noreply.github.com> Date: Thu, 2 Jul 2020 15:01:12 +0200 Subject: [PATCH 03/11] Update double_linear_search.py --- searches/double_linear_search.py | 1 + 1 file changed, 1 insertion(+) diff --git a/searches/double_linear_search.py b/searches/double_linear_search.py index 05c20d3fc7ea..cb2c2a61bb40 100644 --- a/searches/double_linear_search.py +++ b/searches/double_linear_search.py @@ -45,3 +45,4 @@ def double_linear_search(array, x): # print(array) print(double_linear_search(array, x)) + # output: 40 From c7c7bbfddaa0904bbdf3c4c02548cc99b9a2de1c Mon Sep 17 00:00:00 2001 From: Reinhold <63191266+reinhold-b@users.noreply.github.com> Date: Thu, 2 Jul 2020 15:17:50 +0200 Subject: [PATCH 04/11] added doctests --- searches/double_linear_search.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/searches/double_linear_search.py b/searches/double_linear_search.py index cb2c2a61bb40..3171c8d0f3b7 100644 --- a/searches/double_linear_search.py +++ b/searches/double_linear_search.py @@ -13,7 +13,20 @@ def double_linear_search(array, x): """:param array: the array to be searched :param x: the value to be searched (float, int or string) - :returns index of x, if x is in array, else -1 """ + :returns index of x, if x is in array, else -1 + + Examples: + + >>> test_array = [1, 5, 5, 2, 60, 12, 50] + >>> double_linear_search(test_array, 2) + 3 + + >>> double_linear_search(test_array, 5) + 1 + + >>> double_linear_search(test_array, 100) + -1 + """ # define the start and end index of the given array start_ind, end_ind = 0, len(array) - 1 From 04f3980200a8d7711cb47272869842fc30977f40 Mon Sep 17 00:00:00 2001 From: Reinhold <63191266+reinhold-b@users.noreply.github.com> Date: Thu, 2 Jul 2020 15:21:07 +0200 Subject: [PATCH 05/11] updated doctests --- searches/double_linear_search.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/searches/double_linear_search.py b/searches/double_linear_search.py index 3171c8d0f3b7..95c00dffb631 100644 --- a/searches/double_linear_search.py +++ b/searches/double_linear_search.py @@ -17,14 +17,13 @@ def double_linear_search(array, x): Examples: - >>> test_array = [1, 5, 5, 2, 60, 12, 50] - >>> double_linear_search(test_array, 2) - 3 - - >>> double_linear_search(test_array, 5) + >>> double_linear_search([1, 2, 5, 5, 20], 2) 1 - >>> double_linear_search(test_array, 100) + >>> double_linear_search([1, 2, 5, 5, 20], 5) + 2 + + >>> double_linear_search([1, 2, 5, 5, 20], 100) -1 """ From 53fa9de85232b69db5e1edf537f1cec0d71809c6 Mon Sep 17 00:00:00 2001 From: Reinhold <63191266+reinhold-b@users.noreply.github.com> Date: Thu, 2 Jul 2020 15:53:07 +0200 Subject: [PATCH 06/11] Update double_linear_search.py --- searches/double_linear_search.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/searches/double_linear_search.py b/searches/double_linear_search.py index 95c00dffb631..e0c29bf72cdc 100644 --- a/searches/double_linear_search.py +++ b/searches/double_linear_search.py @@ -13,19 +13,19 @@ def double_linear_search(array, x): """:param array: the array to be searched :param x: the value to be searched (float, int or string) - :returns index of x, if x is in array, else -1 + :returns index of x, if x is in array, else -1 Examples: - >>> double_linear_search([1, 2, 5, 5, 20], 2) + >>>double_linear_search([1, 5, 5, 10], 5) 1 - >>> double_linear_search([1, 2, 5, 5, 20], 5) - 2 - - >>> double_linear_search([1, 2, 5, 5, 20], 100) + >>>double_linear_search([1, 5, 5, 10], 100) -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 @@ -57,4 +57,3 @@ def double_linear_search(array, x): # print(array) print(double_linear_search(array, x)) - # output: 40 From fbb366ba15aa02fde0b5d399a1f936c35e788081 Mon Sep 17 00:00:00 2001 From: Reinhold <63191266+reinhold-b@users.noreply.github.com> Date: Thu, 2 Jul 2020 15:59:48 +0200 Subject: [PATCH 07/11] Update double_linear_search.py --- searches/double_linear_search.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/searches/double_linear_search.py b/searches/double_linear_search.py index e0c29bf72cdc..ce0fd66c6228 100644 --- a/searches/double_linear_search.py +++ b/searches/double_linear_search.py @@ -14,15 +14,15 @@ def double_linear_search(array, x): """:param array: the array to be searched :param x: the value to be searched (float, int or string) :returns index of x, if x is in array, else -1 - + Examples: - + >>>double_linear_search([1, 5, 5, 10], 5) 1 - + >>>double_linear_search([1, 5, 5, 10], 100) -1 - + >>>double_linear_search([1, 5, 5, 10], 10) 3 """ From 28f22fb9d330c8d2048f315ceacf3e0472923166 Mon Sep 17 00:00:00 2001 From: Reinhold <63191266+reinhold-b@users.noreply.github.com> Date: Thu, 2 Jul 2020 16:09:55 +0200 Subject: [PATCH 08/11] added blank after >>> --- searches/double_linear_search.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/searches/double_linear_search.py b/searches/double_linear_search.py index ce0fd66c6228..2d6f0412ad76 100644 --- a/searches/double_linear_search.py +++ b/searches/double_linear_search.py @@ -17,13 +17,13 @@ def double_linear_search(array, x): Examples: - >>>double_linear_search([1, 5, 5, 10], 5) + >>> double_linear_search([1, 5, 5, 10], 5) 1 - >>>double_linear_search([1, 5, 5, 10], 100) + >>> double_linear_search([1, 5, 5, 10], 100) -1 - >>>double_linear_search([1, 5, 5, 10], 10) + >>> double_linear_search([1, 5, 5, 10], 10) 3 """ From b6ee5453f80821b592678f6b7c353dedf98b58fd Mon Sep 17 00:00:00 2001 From: Reinhold <63191266+reinhold-b@users.noreply.github.com> Date: Fri, 3 Jul 2020 14:08:35 +0200 Subject: [PATCH 09/11] made all the requested changes --- searches/double_linear_search.py | 34 +++++++++++++++----------------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/searches/double_linear_search.py b/searches/double_linear_search.py index 2d6f0412ad76..9831d1dd0c16 100644 --- a/searches/double_linear_search.py +++ b/searches/double_linear_search.py @@ -1,19 +1,11 @@ """Implementation of a double-linear-search, -which iterates through the array from both sides: start and end. +which iterates through the array from both sides: start and end.""" -Change ARRAY_LENGTH to a number you want to generate an example array, -which goes up to the number you define: -ARRAY_LENGTH = 50 --> [0, 1, 2, ..., 49, 50] -""" -# change for bigger/smaller arrays -ARRAY_LENGTH = 100 - - -def double_linear_search(array, x): +def double_linear_search(array: list, search_item: int) -> int: """:param array: the array to be searched - :param x: the value to be searched (float, int or string) - :returns index of x, if x is in array, else -1 + :param search_item: the item to be searched + :returns index of search_item, if search_item is in array, else -1 Examples: @@ -32,9 +24,9 @@ def double_linear_search(array, x): while start_ind <= end_ind: - if array[start_ind] == x: + if array[start_ind] == search_item: return start_ind - elif array[end_ind] == x: + elif array[end_ind] == search_item: return end_ind else: @@ -47,13 +39,19 @@ def double_linear_search(array, x): if __name__ == "__main__": + """ + Change ARRAY_LENGTH to a number you want to generate an example array, + which goes up to the number you define: + ARRAY_LENGTH = 50 --> [0, 1, 2, ..., 49, 50] + """ + # change for bigger/smaller arrays + ARRAY_LENGTH = 100 + # should be at index 40 - x = 40 + search_item = 40 # this creates an array, which goes up to the number # ARRAY_LENGTH --> [0, 1, ... , 99, 100] array = [x for x in range(ARRAY_LENGTH + 1)] - # print(array) - - print(double_linear_search(array, x)) + print(double_linear_search(array, search_item)) From 05b6e7c46b0320fc030499647e353d995f647b39 Mon Sep 17 00:00:00 2001 From: Reinhold <63191266+reinhold-b@users.noreply.github.com> Date: Fri, 3 Jul 2020 14:13:03 +0200 Subject: [PATCH 10/11] Update double_linear_search.py --- 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 9831d1dd0c16..e309dc88e4cf 100644 --- a/searches/double_linear_search.py +++ b/searches/double_linear_search.py @@ -46,7 +46,7 @@ def double_linear_search(array: list, search_item: int) -> int: """ # change for bigger/smaller arrays ARRAY_LENGTH = 100 - + # should be at index 40 search_item = 40 From 29da1fb6c6ea5ef2ba7b1c2f7a3cc3acc487d8e0 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 4 Jul 2020 11:12:53 +0200 Subject: [PATCH 11/11] Update double_linear_search.py --- searches/double_linear_search.py | 62 +++++++++++--------------------- 1 file changed, 21 insertions(+), 41 deletions(-) diff --git a/searches/double_linear_search.py b/searches/double_linear_search.py index e309dc88e4cf..6056f00fc2bb 100644 --- a/searches/double_linear_search.py +++ b/searches/double_linear_search.py @@ -1,57 +1,37 @@ -"""Implementation of a double-linear-search, -which iterates through the array from both sides: start and end.""" +from typing import List -def double_linear_search(array: list, search_item: int) -> int: - """:param array: the array to be searched - :param search_item: the item to be searched - :returns index of search_item, if search_item is in array, else -1 - - Examples: - - >>> double_linear_search([1, 5, 5, 10], 5) - 1 - - >>> double_linear_search([1, 5, 5, 10], 100) - -1 - - >>> double_linear_search([1, 5, 5, 10], 10) - 3 - """ - +def double_linear_search(array: List[int], search_item: int) -> int: + """ + Iterate through the array from both sides to find the index of search_item. + + :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 + + Examples: + >>> double_linear_search([1, 5, 5, 10], 1) + 0 + >>> double_linear_search([1, 5, 5, 10], 5) + 1 + >>> double_linear_search([1, 5, 5, 10], 100) + -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: - # increase start_ind, decrease end_ind start_ind += 1 end_ind -= 1 - - # returns -1 if the element x is not found in the given array + # returns -1 if search_item is not found in array return -1 if __name__ == "__main__": - """ - Change ARRAY_LENGTH to a number you want to generate an example array, - which goes up to the number you define: - ARRAY_LENGTH = 50 --> [0, 1, 2, ..., 49, 50] - """ - # change for bigger/smaller arrays - ARRAY_LENGTH = 100 - - # should be at index 40 - search_item = 40 - - # this creates an array, which goes up to the number - # ARRAY_LENGTH --> [0, 1, ... , 99, 100] - array = [x for x in range(ARRAY_LENGTH + 1)] - - print(double_linear_search(array, search_item)) + print(double_linear_search(list(range(100)), 40))