From cbfc01b798b8f39eebbdadd97d3e2898c1a6c5e4 Mon Sep 17 00:00:00 2001 From: Pedram_Mohajer <48964282+pedram-mohajer@users.noreply.github.com> Date: Sun, 26 Nov 2023 19:31:53 -0500 Subject: [PATCH 1/6] Create find_median_sorted_arrays.py --- .../arrays/find_median_sorted_arrays.py | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 data_structures/arrays/find_median_sorted_arrays.py diff --git a/data_structures/arrays/find_median_sorted_arrays.py b/data_structures/arrays/find_median_sorted_arrays.py new file mode 100644 index 000000000000..ed804ff5f5e6 --- /dev/null +++ b/data_structures/arrays/find_median_sorted_arrays.py @@ -0,0 +1,77 @@ +def find_median_sorted_arrays(nums1: list[int], nums2: list[int]) -> float: + """ + Finds the median of two sorted arrays. + + :param nums1: The first sorted array. + :param nums2: The second sorted array. + :return: The median of the combined sorted arrays. + :raises ValueError: If both input arrays are empty. + + >>> find_median_sorted_arrays([1, 3], [2]) + 2.0 + >>> find_median_sorted_arrays([1, 2], [3, 4]) + 2.5 + >>> find_median_sorted_arrays([1, 3, 5], [2, 4, 6]) + 3.5 + >>> find_median_sorted_arrays([1, 2, 3], [4, 5, 6, 7]) + 4.0 + >>> find_median_sorted_arrays([], [2, 3]) + 2.5 + + """ + # Check if both input arrays are empty + if not nums1 and not nums2: + raise ValueError("Input arrays are not valid") + + # If one is empty, find the median of the non-empty array + if not nums1 or not nums2: + combined = nums1 if nums1 else nums2 + mid = len(combined) // 2 + if len(combined) % 2 == 0: + return (combined[mid - 1] + combined[mid]) / 2.0 + else: + return combined[mid] + + # Make sure nums1 is the smaller array + if len(nums1) > len(nums2): + nums1, nums2 = nums2, nums1 + + m, n = len(nums1), len(nums2) + total = m + n + half = total // 2 + + left, right = 0, m + while left <= right: + partition1 = (left + right) // 2 + partition2 = half - partition1 + + max_left1 = float("-inf") if partition1 == 0 else nums1[partition1 - 1] + min_right1 = float("inf") if partition1 == m else nums1[partition1] + + max_left2 = float("-inf") if partition2 == 0 else nums2[partition2 - 1] + min_right2 = float("inf") if partition2 == n else nums2[partition2] + + if max_left1 <= min_right2 and max_left2 <= min_right1: + if total % 2 == 0: + return float( + (max(max_left1, max_left2) + min(min_right1, min_right2)) / 2 + ) + else: + return float(min(min_right1, min_right2)) + elif max_left1 > min_right2: + right = partition1 - 1 + else: + left = partition1 + 1 + + # If the loop exits without returning, the input arrays are not valid + raise ValueError("Input arrays are not valid") + +# Example +if __name__ == "__main__": + nums1 = [1, 3] + nums2 = [2] + print("Median of Example 1:", find_median_sorted_arrays(nums1, nums2)) + + nums1 = [1, 2] + nums2 = [3, 4] + print("Median of Example 2:", find_median_sorted_arrays(nums1, nums2)) From 22e4f758a506d58b2694ddf9b991a12237728164 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 27 Nov 2023 00:37:20 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/find_median_sorted_arrays.py | 1 + 1 file changed, 1 insertion(+) diff --git a/data_structures/arrays/find_median_sorted_arrays.py b/data_structures/arrays/find_median_sorted_arrays.py index ed804ff5f5e6..6f068ef11229 100644 --- a/data_structures/arrays/find_median_sorted_arrays.py +++ b/data_structures/arrays/find_median_sorted_arrays.py @@ -66,6 +66,7 @@ def find_median_sorted_arrays(nums1: list[int], nums2: list[int]) -> float: # If the loop exits without returning, the input arrays are not valid raise ValueError("Input arrays are not valid") + # Example if __name__ == "__main__": nums1 = [1, 3] From 2b651143a7e1fb990cee060c16072cb5329a6292 Mon Sep 17 00:00:00 2001 From: Pedram_Mohajer <48964282+pedram-mohajer@users.noreply.github.com> Date: Sun, 26 Nov 2023 19:42:21 -0500 Subject: [PATCH 3/6] Update find_median_sorted_arrays.py --- data_structures/arrays/find_median_sorted_arrays.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/data_structures/arrays/find_median_sorted_arrays.py b/data_structures/arrays/find_median_sorted_arrays.py index 6f068ef11229..a63671e55c32 100644 --- a/data_structures/arrays/find_median_sorted_arrays.py +++ b/data_structures/arrays/find_median_sorted_arrays.py @@ -1,3 +1,7 @@ +""" +https://atharayil.medium.com/median-of-two-sorted-arrays-day-36-python-fcbd2dbbb668 +""" + def find_median_sorted_arrays(nums1: list[int], nums2: list[int]) -> float: """ Finds the median of two sorted arrays. From 7c44c26165d44bb7e9eb8c03c3201a550098a656 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 27 Nov 2023 00:42:53 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/find_median_sorted_arrays.py | 1 + 1 file changed, 1 insertion(+) diff --git a/data_structures/arrays/find_median_sorted_arrays.py b/data_structures/arrays/find_median_sorted_arrays.py index a63671e55c32..2fe833a6b4d9 100644 --- a/data_structures/arrays/find_median_sorted_arrays.py +++ b/data_structures/arrays/find_median_sorted_arrays.py @@ -2,6 +2,7 @@ https://atharayil.medium.com/median-of-two-sorted-arrays-day-36-python-fcbd2dbbb668 """ + def find_median_sorted_arrays(nums1: list[int], nums2: list[int]) -> float: """ Finds the median of two sorted arrays. From e60b6bbdb9102e944ddeee8e3f4cd40a945d878f Mon Sep 17 00:00:00 2001 From: Pedram_Mohajer <48964282+pedram-mohajer@users.noreply.github.com> Date: Thu, 30 Nov 2023 09:52:57 -0500 Subject: [PATCH 5/6] Update find_median_sorted_arrays.py --- .../arrays/find_median_sorted_arrays.py | 90 +++++++++---------- 1 file changed, 41 insertions(+), 49 deletions(-) diff --git a/data_structures/arrays/find_median_sorted_arrays.py b/data_structures/arrays/find_median_sorted_arrays.py index 2fe833a6b4d9..84bd7d04a81b 100644 --- a/data_structures/arrays/find_median_sorted_arrays.py +++ b/data_structures/arrays/find_median_sorted_arrays.py @@ -2,15 +2,14 @@ https://atharayil.medium.com/median-of-two-sorted-arrays-day-36-python-fcbd2dbbb668 """ - def find_median_sorted_arrays(nums1: list[int], nums2: list[int]) -> float: """ - Finds the median of two sorted arrays. + Find the median of two sorted integer arrays. - :param nums1: The first sorted array. - :param nums2: The second sorted array. + :param nums1: The first sorted array (must be integers). + :param nums2: The second sorted array (must be integers). :return: The median of the combined sorted arrays. - :raises ValueError: If both input arrays are empty. + :raises ValueError: if both are empty, not sorted, or contain non-integer values. >>> find_median_sorted_arrays([1, 3], [2]) 2.0 @@ -22,62 +21,55 @@ def find_median_sorted_arrays(nums1: list[int], nums2: list[int]) -> float: 4.0 >>> find_median_sorted_arrays([], [2, 3]) 2.5 - + >>> find_median_sorted_arrays([-2, -1], [0, 2]) + -0.5 + >>> find_median_sorted_arrays([1.5, 2.5], [3.0, 4.0]) # Contains a float + Traceback (most recent call last): + ... + ValueError: Input arrays must be sorted and contain only integer values + >>> find_median_sorted_arrays([3, 2, 1], [4, 5, 6]) # Not sorted + Traceback (most recent call last): + ... + ValueError: Input arrays must be sorted and contain only integer values + >>> find_median_sorted_arrays([1, 2, 'a'], [3, 4, 5]) # Contains a character + Traceback (most recent call last): + ... + ValueError: Input arrays must be sorted and contain only integer values + >>> find_median_sorted_arrays(['a', 'b', 'c'], [3, 4, 5]) # Contains a character + Traceback (most recent call last): + ... + ValueError: Input arrays must be sorted and contain only integer values """ # Check if both input arrays are empty if not nums1 and not nums2: - raise ValueError("Input arrays are not valid") - - # If one is empty, find the median of the non-empty array - if not nums1 or not nums2: - combined = nums1 if nums1 else nums2 - mid = len(combined) // 2 - if len(combined) % 2 == 0: - return (combined[mid - 1] + combined[mid]) / 2.0 - else: - return combined[mid] - - # Make sure nums1 is the smaller array - if len(nums1) > len(nums2): - nums1, nums2 = nums2, nums1 - - m, n = len(nums1), len(nums2) - total = m + n - half = total // 2 - - left, right = 0, m - while left <= right: - partition1 = (left + right) // 2 - partition2 = half - partition1 - - max_left1 = float("-inf") if partition1 == 0 else nums1[partition1 - 1] - min_right1 = float("inf") if partition1 == m else nums1[partition1] + raise ValueError("Input arrays are empty") - max_left2 = float("-inf") if partition2 == 0 else nums2[partition2 - 1] - min_right2 = float("inf") if partition2 == n else nums2[partition2] + # Check if arrays are sorted and contain only integer values + for array in [nums1, nums2]: + if any(not isinstance(x, int) for x in array) or any( + array[i] > array[i + 1] for i in range(len(array) - 1) + ): + raise ValueError( + "Input arrays must be sorted and contain only integer values" + ) - if max_left1 <= min_right2 and max_left2 <= min_right1: - if total % 2 == 0: - return float( - (max(max_left1, max_left2) + min(min_right1, min_right2)) / 2 - ) - else: - return float(min(min_right1, min_right2)) - elif max_left1 > min_right2: - right = partition1 - 1 - else: - left = partition1 + 1 + # Combine and sort the arrays + combined = sorted(nums1 + nums2) - # If the loop exits without returning, the input arrays are not valid - raise ValueError("Input arrays are not valid") + # Calculate the median + mid = len(combined) // 2 + if len(combined) % 2 == 0: + return float((combined[mid - 1] + combined[mid]) / 2.0) + else: + return float(combined[mid]) -# Example +# Example usage if __name__ == "__main__": nums1 = [1, 3] nums2 = [2] print("Median of Example 1:", find_median_sorted_arrays(nums1, nums2)) - nums1 = [1, 2] + nums1 = [-4, -2] nums2 = [3, 4] print("Median of Example 2:", find_median_sorted_arrays(nums1, nums2)) From ba0db110295333646bf772060e8b212550fd6221 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 30 Nov 2023 14:54:26 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/find_median_sorted_arrays.py | 1 + 1 file changed, 1 insertion(+) diff --git a/data_structures/arrays/find_median_sorted_arrays.py b/data_structures/arrays/find_median_sorted_arrays.py index 84bd7d04a81b..b473f67f281c 100644 --- a/data_structures/arrays/find_median_sorted_arrays.py +++ b/data_structures/arrays/find_median_sorted_arrays.py @@ -2,6 +2,7 @@ https://atharayil.medium.com/median-of-two-sorted-arrays-day-36-python-fcbd2dbbb668 """ + def find_median_sorted_arrays(nums1: list[int], nums2: list[int]) -> float: """ Find the median of two sorted integer arrays.