From c0b6798952b03fc7366d791c5f0c0fa51908d8d9 Mon Sep 17 00:00:00 2001 From: Hariharan Date: Tue, 15 Oct 2024 20:03:04 +0530 Subject: [PATCH] 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: