From 71b0f857d1e6c145350de607110617f7d573d300 Mon Sep 17 00:00:00 2001 From: Anurag Shukla <76862299+anuragshuklajec@users.noreply.github.com> Date: Tue, 11 Oct 2022 17:51:59 +0530 Subject: [PATCH 1/6] Create binary_search_matrix.py Added an algorithm to search in matrix --- matrix/binary_search_matrix.py | 51 ++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 matrix/binary_search_matrix.py diff --git a/matrix/binary_search_matrix.py b/matrix/binary_search_matrix.py new file mode 100644 index 000000000000..f8c5fb3bc605 --- /dev/null +++ b/matrix/binary_search_matrix.py @@ -0,0 +1,51 @@ +def binary_search(array: list,lower_bound: int,upper_bound: int, value: int) -> int: + """ + This function carries out Binary search on a 1d array and + return -1 if it do not exist + array: A 1d sorted array + value : the value meant to be searched + >>> matrix = [1, 4, 7, 11, 15] + >>> binary_search(matrix, 0, len(matrix) - 1, 1) + 0 + >>> binary_search(matrix, 0, len(matrix) - 1, 23) + -1 + """ + + r = int((lower_bound + upper_bound) // 2) + if array[r] == value: + return r + if lower_bound >= upper_bound: + return -1 + if array[r] < value: + return binary_search(array,r+1,upper_bound, value) + else: + return binary_search(array,lower_bound,r-1, value) + + +def mat_bin_search(value: int, matrix: list) -> list: + """ + This function loops over a 2d matrix and calls binarySearch on + the selected 1d array and returns [-1, -1] is it do not exist + value : value meant to be searched + matrix = a sorted 2d matrix + >>> matrix = [[1, 4, 7, 11, 15], + ... [2, 5, 8, 12, 19], + ... [3, 6, 9, 16, 22], + ... [10, 13, 14, 17, 24], + ... [18, 21, 23, 26, 30]] + >>> target = 1 + >>> mat_bin_search(target, matrix) + [0, 0] + >>> target = 34 + >>> mat_bin_search(target, matrix) + [-1, -1] + """ + index = 0 + if matrix[index][0] == value: + return [index, 0] + while index < len(matrix) and matrix[index][0] < value : + r = binary_search(matrix[index],0,len(matrix[index])-1, value) + if r != -1: + return [index, r] + index += 1 + return [-1, -1] From b889a903800e1ea4192132b4991e298a13c19bf3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 11 Oct 2022 12:29:18 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- matrix/binary_search_matrix.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/matrix/binary_search_matrix.py b/matrix/binary_search_matrix.py index f8c5fb3bc605..09c59362f6c0 100644 --- a/matrix/binary_search_matrix.py +++ b/matrix/binary_search_matrix.py @@ -1,4 +1,4 @@ -def binary_search(array: list,lower_bound: int,upper_bound: int, value: int) -> int: +def binary_search(array: list, lower_bound: int, upper_bound: int, value: int) -> int: """ This function carries out Binary search on a 1d array and return -1 if it do not exist @@ -17,9 +17,9 @@ def binary_search(array: list,lower_bound: int,upper_bound: int, value: int) -> if lower_bound >= upper_bound: return -1 if array[r] < value: - return binary_search(array,r+1,upper_bound, value) + return binary_search(array, r + 1, upper_bound, value) else: - return binary_search(array,lower_bound,r-1, value) + return binary_search(array, lower_bound, r - 1, value) def mat_bin_search(value: int, matrix: list) -> list: @@ -43,8 +43,8 @@ def mat_bin_search(value: int, matrix: list) -> list: index = 0 if matrix[index][0] == value: return [index, 0] - while index < len(matrix) and matrix[index][0] < value : - r = binary_search(matrix[index],0,len(matrix[index])-1, value) + while index < len(matrix) and matrix[index][0] < value: + r = binary_search(matrix[index], 0, len(matrix[index]) - 1, value) if r != -1: return [index, r] index += 1 From 62a23c45a109bd6fcb4a29d27ea4ff5b4b563c48 Mon Sep 17 00:00:00 2001 From: Anurag Shukla <76862299+anuragshuklajec@users.noreply.github.com> Date: Tue, 11 Oct 2022 23:59:13 +0530 Subject: [PATCH 3/6] Update binary_search_matrix.py --- matrix/binary_search_matrix.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/matrix/binary_search_matrix.py b/matrix/binary_search_matrix.py index 09c59362f6c0..6c6af3a4694c 100644 --- a/matrix/binary_search_matrix.py +++ b/matrix/binary_search_matrix.py @@ -49,3 +49,8 @@ def mat_bin_search(value: int, matrix: list) -> list: return [index, r] index += 1 return [-1, -1] + + if __name__ == "__main__": + import doctest + + doctest.testmod() From 1252572afbe7157239f64fb2d37ce48af2b79b0b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 11 Oct 2022 18:32:45 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- matrix/binary_search_matrix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix/binary_search_matrix.py b/matrix/binary_search_matrix.py index 6c6af3a4694c..bf26d8a603f7 100644 --- a/matrix/binary_search_matrix.py +++ b/matrix/binary_search_matrix.py @@ -49,7 +49,7 @@ def mat_bin_search(value: int, matrix: list) -> list: return [index, r] index += 1 return [-1, -1] - + if __name__ == "__main__": import doctest From 805b7ed9cc4ef6b7e1e63bafc154db05f991fd5f Mon Sep 17 00:00:00 2001 From: Anurag Shukla <76862299+anuragshuklajec@users.noreply.github.com> Date: Wed, 12 Oct 2022 00:05:21 +0530 Subject: [PATCH 5/6] fix Indentation --- matrix/binary_search_matrix.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/matrix/binary_search_matrix.py b/matrix/binary_search_matrix.py index bf26d8a603f7..c9c0d5f7ae2a 100644 --- a/matrix/binary_search_matrix.py +++ b/matrix/binary_search_matrix.py @@ -51,6 +51,6 @@ def mat_bin_search(value: int, matrix: list) -> list: return [-1, -1] if __name__ == "__main__": - import doctest + import doctest - doctest.testmod() + doctest.testmod() From c400ed04c88304fdeb3919b2b2b2cd8e9b289504 Mon Sep 17 00:00:00 2001 From: Anurag Shukla <76862299+anuragshuklajec@users.noreply.github.com> Date: Fri, 14 Oct 2022 00:52:02 +0530 Subject: [PATCH 6/6] Update matrix/binary_search_matrix.py Co-authored-by: Christian Clauss --- matrix/binary_search_matrix.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/matrix/binary_search_matrix.py b/matrix/binary_search_matrix.py index c9c0d5f7ae2a..6f203b7a3484 100644 --- a/matrix/binary_search_matrix.py +++ b/matrix/binary_search_matrix.py @@ -50,7 +50,8 @@ def mat_bin_search(value: int, matrix: list) -> list: index += 1 return [-1, -1] - if __name__ == "__main__": - import doctest - doctest.testmod() +if __name__ == "__main__": + import doctest + + doctest.testmod()