From eeeb9ab48a5aa0ed1dafde0694e6c1093252f4a3 Mon Sep 17 00:00:00 2001 From: Hariharan Date: Tue, 15 Oct 2024 19:33:08 +0530 Subject: [PATCH 1/8] Add files via upload --- sorts/BinarySearch_on_Sorted_2D_Array.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 sorts/BinarySearch_on_Sorted_2D_Array.py diff --git a/sorts/BinarySearch_on_Sorted_2D_Array.py b/sorts/BinarySearch_on_Sorted_2D_Array.py new file mode 100644 index 000000000..e69de29bb From 03adefc12ab8c0c19b4a44228e88cf555a899719 Mon Sep 17 00:00:00 2001 From: Hariharan Date: Tue, 15 Oct 2024 19:40:42 +0530 Subject: [PATCH 2/8] Add files via upload --- searches/Peak_Element_on_2D.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 searches/Peak_Element_on_2D.py diff --git a/searches/Peak_Element_on_2D.py b/searches/Peak_Element_on_2D.py new file mode 100644 index 000000000..af7b01ec8 --- /dev/null +++ b/searches/Peak_Element_on_2D.py @@ -0,0 +1,33 @@ +def find_peak_util(matrix, left, right, row_count): + mid_col = (left + right) // 2 + + max_row_index = 0 + for i in range(row_count): + if matrix[i][mid_col] > matrix[max_row_index][mid_col]: + max_row_index = i + + + if (mid_col == 0 or matrix[max_row_index][mid_col] >= matrix[max_row_index][mid_col - 1]) and \ + (mid_col == len(matrix[0]) - 1 or matrix[max_row_index][mid_col] >= matrix[max_row_index][mid_col + 1]): + return matrix[max_row_index][mid_col] + + + if mid_col > 0 and matrix[max_row_index][mid_col - 1] > matrix[max_row_index][mid_col]: + return find_peak_util(matrix, left, mid_col - 1, row_count) + + return find_peak_util(matrix, mid_col + 1, right, row_count) + +def find_peak(matrix): + if not matrix or not matrix[0]: + return None + return find_peak_util(matrix, 0, len(matrix[0]) - 1, len(matrix)) + +matrix = [ + [10, 8, 10, 10], + [14, 13, 12, 11], + [15, 9, 11, 21], + [16, 17, 19, 20] +] + +peak = find_peak(matrix) +print(f"Peak element is: {peak}") From 14f8cf47ec67bce58273dc23a0d78779e6666f63 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 15 Oct 2024 14:17:25 +0000 Subject: [PATCH 3/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- searches/Peak_Element_on_2D.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/searches/Peak_Element_on_2D.py b/searches/Peak_Element_on_2D.py index af7b01ec8..2c4a05d42 100644 --- a/searches/Peak_Element_on_2D.py +++ b/searches/Peak_Element_on_2D.py @@ -1,33 +1,36 @@ def find_peak_util(matrix, left, right, row_count): mid_col = (left + right) // 2 - + max_row_index = 0 for i in range(row_count): if matrix[i][mid_col] > matrix[max_row_index][mid_col]: max_row_index = i - - if (mid_col == 0 or matrix[max_row_index][mid_col] >= matrix[max_row_index][mid_col - 1]) and \ - (mid_col == len(matrix[0]) - 1 or matrix[max_row_index][mid_col] >= matrix[max_row_index][mid_col + 1]): + if ( + mid_col == 0 + or matrix[max_row_index][mid_col] >= matrix[max_row_index][mid_col - 1] + ) and ( + mid_col == len(matrix[0]) - 1 + or matrix[max_row_index][mid_col] >= matrix[max_row_index][mid_col + 1] + ): return matrix[max_row_index][mid_col] - - if mid_col > 0 and matrix[max_row_index][mid_col - 1] > matrix[max_row_index][mid_col]: + if ( + mid_col > 0 + and matrix[max_row_index][mid_col - 1] > matrix[max_row_index][mid_col] + ): return find_peak_util(matrix, left, mid_col - 1, row_count) return find_peak_util(matrix, mid_col + 1, right, row_count) + def find_peak(matrix): if not matrix or not matrix[0]: return None return find_peak_util(matrix, 0, len(matrix[0]) - 1, len(matrix)) -matrix = [ - [10, 8, 10, 10], - [14, 13, 12, 11], - [15, 9, 11, 21], - [16, 17, 19, 20] -] + +matrix = [[10, 8, 10, 10], [14, 13, 12, 11], [15, 9, 11, 21], [16, 17, 19, 20]] peak = find_peak(matrix) print(f"Peak element is: {peak}") From 5afd15a9fe0bb625ae024bb3f11a524a2864c338 Mon Sep 17 00:00:00 2001 From: Hariharan Date: Tue, 15 Oct 2024 19:50:27 +0530 Subject: [PATCH 4/8] Update and rename Peak_Element_on_2D.py to peakelementin2D.py --- searches/{Peak_Element_on_2D.py => peakelementin2D.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename searches/{Peak_Element_on_2D.py => peakelementin2D.py} (100%) diff --git a/searches/Peak_Element_on_2D.py b/searches/peakelementin2D.py similarity index 100% rename from searches/Peak_Element_on_2D.py rename to searches/peakelementin2D.py From 3da098ba6f5327df8d6648183d8bc229187bbd86 Mon Sep 17 00:00:00 2001 From: Hariharan Date: Tue, 15 Oct 2024 19:59:26 +0530 Subject: [PATCH 5/8] Update BinarySearch_on_Sorted_2D_Array.py --- sorts/BinarySearch_on_Sorted_2D_Array.py | 50 ++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/sorts/BinarySearch_on_Sorted_2D_Array.py b/sorts/BinarySearch_on_Sorted_2D_Array.py index e69de29bb..fcce1d795 100644 --- a/sorts/BinarySearch_on_Sorted_2D_Array.py +++ b/sorts/BinarySearch_on_Sorted_2D_Array.py @@ -0,0 +1,50 @@ +def binary_search_2d(matrix: list[list[int]], target: int) -> tuple[int, int]: + """ + Searches for a target value in a 2D sorted array. + + The matrix is sorted such that each row is in ascending order, and the + first element of each row is greater than the last element of the previous row. + + :param matrix: A 2D list of integers sorted in ascending order. + :param target: The integer value to search for. + :return: A tuple (row_index, col_index) if found, otherwise (-1, -1). + + Examples: + >>> binary_search_2d([[1, 3, 5], [7, 9, 11], [12, 13, 15]], 9) + (1, 1) + >>> binary_search_2d([[1, 3, 5], [7, 9, 11], [12, 13, 15]], 4) + (-1, -1) + """ + if not matrix or not matrix[0]: + return -1, -1 + + rows, cols = len(matrix), len(matrix[0]) + left, right = 0, rows * cols - 1 + + while left <= right: + mid = left + (right - left) // 2 + mid_value = matrix[mid // cols][mid % cols] + + if mid_value == target: + return mid // cols, mid % cols + elif mid_value < target: + left = mid + 1 + else: + right = mid - 1 + + return -1, -1 + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + + # Example usage + matrix = [[1, 3, 5], [7, 9, 11], [12, 13, 15]] + target = 9 + result = binary_search_2d(matrix, target) + if result == (-1, -1): + print(f"{target} was not found in the matrix.") + else: + print(f"{target} was found at position {result} in the matrix.") From c0b6798952b03fc7366d791c5f0c0fa51908d8d9 Mon Sep 17 00:00:00 2001 From: Hariharan Date: Tue, 15 Oct 2024 20:03:04 +0530 Subject: [PATCH 6/8] Update and rename BinarySearch_on_Sorted_2D_Array.py to binarysearch_on_sorted_2d_array.py --- ....py => binarysearch_on_sorted_2d_array.py} | 36 +++++++++++++++---- 1 file changed, 29 insertions(+), 7 deletions(-) rename sorts/{BinarySearch_on_Sorted_2D_Array.py => binarysearch_on_sorted_2d_array.py} (57%) diff --git a/sorts/BinarySearch_on_Sorted_2D_Array.py b/sorts/binarysearch_on_sorted_2d_array.py similarity index 57% rename from sorts/BinarySearch_on_Sorted_2D_Array.py rename to sorts/binarysearch_on_sorted_2d_array.py index fcce1d795..3dca954ac 100644 --- a/sorts/BinarySearch_on_Sorted_2D_Array.py +++ b/sorts/binarysearch_on_sorted_2d_array.py @@ -1,3 +1,6 @@ +#!/usr/bin/env python3 + + def binary_search_2d(matrix: list[list[int]], target: int) -> tuple[int, int]: """ Searches for a target value in a 2D sorted array. @@ -5,20 +8,39 @@ def binary_search_2d(matrix: list[list[int]], target: int) -> tuple[int, int]: The matrix is sorted such that each row is in ascending order, and the first element of each row is greater than the last element of the previous row. - :param matrix: A 2D list of integers sorted in ascending order. - :param target: The integer value to search for. - :return: A tuple (row_index, col_index) if found, otherwise (-1, -1). + Args: + matrix: A 2D list of integers sorted in ascending order. + target: The integer value to search for. + + Returns: + A tuple (row_index, col_index) if found, otherwise (-1, -1). + + Raises: + ValueError: If matrix is empty or not rectangular. - Examples: >>> binary_search_2d([[1, 3, 5], [7, 9, 11], [12, 13, 15]], 9) (1, 1) >>> binary_search_2d([[1, 3, 5], [7, 9, 11], [12, 13, 15]], 4) (-1, -1) + >>> binary_search_2d([], 1) # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + ValueError: matrix must not be empty + >>> binary_search_2d([[1, 2], [3, 4, 5]], 3) # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + ValueError: matrix must be rectangular """ if not matrix or not matrix[0]: - return -1, -1 - - rows, cols = len(matrix), len(matrix[0]) + raise ValueError("matrix must not be empty") + + rows = len(matrix) + cols = len(matrix[0]) + + for row in matrix: + if len(row) != cols: + raise ValueError("matrix must be rectangular") + left, right = 0, rows * cols - 1 while left <= right: From ac0008a5230faade3fdd9d5249e5714c85aaffc3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 15 Oct 2024 14:33:39 +0000 Subject: [PATCH 7/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/binarysearch_on_sorted_2d_array.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sorts/binarysearch_on_sorted_2d_array.py b/sorts/binarysearch_on_sorted_2d_array.py index 3dca954ac..b5c4e95a1 100644 --- a/sorts/binarysearch_on_sorted_2d_array.py +++ b/sorts/binarysearch_on_sorted_2d_array.py @@ -33,14 +33,14 @@ def binary_search_2d(matrix: list[list[int]], target: int) -> tuple[int, int]: """ if not matrix or not matrix[0]: raise ValueError("matrix must not be empty") - + rows = len(matrix) cols = len(matrix[0]) - + for row in matrix: if len(row) != cols: raise ValueError("matrix must be rectangular") - + left, right = 0, rows * cols - 1 while left <= right: From aba42dce0a698542c4715c4760c947a29ea353d3 Mon Sep 17 00:00:00 2001 From: Hariharan Date: Tue, 15 Oct 2024 20:03:42 +0530 Subject: [PATCH 8/8] Delete searches/peakelementin2D.py --- searches/peakelementin2D.py | 36 ------------------------------------ 1 file changed, 36 deletions(-) delete mode 100644 searches/peakelementin2D.py diff --git a/searches/peakelementin2D.py b/searches/peakelementin2D.py deleted file mode 100644 index 2c4a05d42..000000000 --- a/searches/peakelementin2D.py +++ /dev/null @@ -1,36 +0,0 @@ -def find_peak_util(matrix, left, right, row_count): - mid_col = (left + right) // 2 - - max_row_index = 0 - for i in range(row_count): - if matrix[i][mid_col] > matrix[max_row_index][mid_col]: - max_row_index = i - - if ( - mid_col == 0 - or matrix[max_row_index][mid_col] >= matrix[max_row_index][mid_col - 1] - ) and ( - mid_col == len(matrix[0]) - 1 - or matrix[max_row_index][mid_col] >= matrix[max_row_index][mid_col + 1] - ): - return matrix[max_row_index][mid_col] - - if ( - mid_col > 0 - and matrix[max_row_index][mid_col - 1] > matrix[max_row_index][mid_col] - ): - return find_peak_util(matrix, left, mid_col - 1, row_count) - - return find_peak_util(matrix, mid_col + 1, right, row_count) - - -def find_peak(matrix): - if not matrix or not matrix[0]: - return None - return find_peak_util(matrix, 0, len(matrix[0]) - 1, len(matrix)) - - -matrix = [[10, 8, 10, 10], [14, 13, 12, 11], [15, 9, 11, 21], [16, 17, 19, 20]] - -peak = find_peak(matrix) -print(f"Peak element is: {peak}")