From 9d47c8773181de6c727f9fe4590e52a6fea1710e Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Mon, 2 Oct 2023 12:04:26 +0530 Subject: [PATCH 01/12] add median of two sorted array --- data_structures/arrays/median_two_array.py | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 data_structures/arrays/median_two_array.py diff --git a/data_structures/arrays/median_two_array.py b/data_structures/arrays/median_two_array.py new file mode 100644 index 000000000000..63bde968e9cc --- /dev/null +++ b/data_structures/arrays/median_two_array.py @@ -0,0 +1,45 @@ +class Solution: + def find_median_sorted_arrays(self, nums1: list[int], nums2: list[int]) -> float: + """ + Find the median of two sorted arrays. + + Args: + nums1: The first sorted array. + nums2: The second sorted array. + + Returns: + The median of the two sorted arrays. + + Example: + >>> sol = Solution() + >>> sol.find_median_sorted_arrays([1, 3], [2]) + 2.0 + + >>> sol.find_median_sorted_arrays([1, 2], [3, 4]) + 2.5 + + >>> sol.find_median_sorted_arrays([0, 0], [0, 0]) + 0.0 + """ + # Merge the arrays into a single sorted array. + merged = nums1 + nums2 + + # Sort the merged array. + merged.sort() + + # Calculate the total number of elements in the merged array. + total = len(merged) + + if total % 2 == 1: + # If the total number of elements is odd, return the middle element as the median. + return float(merged[total // 2]) + else: + # If the total number of elements is even, calculate the average of the two middle elements as the median. + middle1 = merged[total // 2 - 1] + middle2 = merged[total // 2] + return (float(middle1) + float(middle2)) / 2.0 + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 8981a8e1b61e3a068a7e789f18c64154a4de8ae6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 2 Oct 2023 06:36:22 +0000 Subject: [PATCH 02/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/median_two_array.py | 1 + 1 file changed, 1 insertion(+) diff --git a/data_structures/arrays/median_two_array.py b/data_structures/arrays/median_two_array.py index 63bde968e9cc..d0ef1d7b67af 100644 --- a/data_structures/arrays/median_two_array.py +++ b/data_structures/arrays/median_two_array.py @@ -39,6 +39,7 @@ def find_median_sorted_arrays(self, nums1: list[int], nums2: list[int]) -> float middle2 = merged[total // 2] return (float(middle1) + float(middle2)) / 2.0 + if __name__ == "__main__": import doctest From ab0ecd420f97afc957de6873446506bf14bbfa46 Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Mon, 2 Oct 2023 12:08:50 +0530 Subject: [PATCH 03/12] fix syntax --- data_structures/arrays/median_two_array.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/data_structures/arrays/median_two_array.py b/data_structures/arrays/median_two_array.py index d0ef1d7b67af..fc29f22ec77f 100644 --- a/data_structures/arrays/median_two_array.py +++ b/data_structures/arrays/median_two_array.py @@ -31,15 +31,16 @@ def find_median_sorted_arrays(self, nums1: list[int], nums2: list[int]) -> float total = len(merged) if total % 2 == 1: - # If the total number of elements is odd, return the middle element as the median. + # If the total number of elements is odd, return the + # middle element as the median. return float(merged[total // 2]) else: - # If the total number of elements is even, calculate the average of the two middle elements as the median. + # If the total number of elements is even, calculate + # the average of the two middle elements as the median. middle1 = merged[total // 2 - 1] middle2 = merged[total // 2] return (float(middle1) + float(middle2)) / 2.0 - if __name__ == "__main__": import doctest From fdc5914d39b6b88f40fe7cfe4f9ad0e8e8f5939e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 2 Oct 2023 06:39:57 +0000 Subject: [PATCH 04/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/median_two_array.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/data_structures/arrays/median_two_array.py b/data_structures/arrays/median_two_array.py index fc29f22ec77f..4f63e3accf94 100644 --- a/data_structures/arrays/median_two_array.py +++ b/data_structures/arrays/median_two_array.py @@ -31,16 +31,17 @@ def find_median_sorted_arrays(self, nums1: list[int], nums2: list[int]) -> float total = len(merged) if total % 2 == 1: - # If the total number of elements is odd, return the + # If the total number of elements is odd, return the # middle element as the median. return float(merged[total // 2]) else: - # If the total number of elements is even, calculate + # If the total number of elements is even, calculate # the average of the two middle elements as the median. middle1 = merged[total // 2 - 1] middle2 = merged[total // 2] return (float(middle1) + float(middle2)) / 2.0 + if __name__ == "__main__": import doctest From 807761845453f31fbb6941d3f302ed762e7a02cb Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Mon, 2 Oct 2023 12:10:10 +0530 Subject: [PATCH 05/12] fix syntax --- data_structures/arrays/median_two_array.py | 1 - 1 file changed, 1 deletion(-) diff --git a/data_structures/arrays/median_two_array.py b/data_structures/arrays/median_two_array.py index 4f63e3accf94..460847e0cf88 100644 --- a/data_structures/arrays/median_two_array.py +++ b/data_structures/arrays/median_two_array.py @@ -41,7 +41,6 @@ def find_median_sorted_arrays(self, nums1: list[int], nums2: list[int]) -> float middle2 = merged[total // 2] return (float(middle1) + float(middle2)) / 2.0 - if __name__ == "__main__": import doctest From 97e335d94948f7e551a34cb0f74e081992e7585e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 2 Oct 2023 06:41:39 +0000 Subject: [PATCH 06/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/median_two_array.py | 1 + 1 file changed, 1 insertion(+) diff --git a/data_structures/arrays/median_two_array.py b/data_structures/arrays/median_two_array.py index 460847e0cf88..4f63e3accf94 100644 --- a/data_structures/arrays/median_two_array.py +++ b/data_structures/arrays/median_two_array.py @@ -41,6 +41,7 @@ def find_median_sorted_arrays(self, nums1: list[int], nums2: list[int]) -> float middle2 = merged[total // 2] return (float(middle1) + float(middle2)) / 2.0 + if __name__ == "__main__": import doctest From 2bad9c1ad889c0bff8673875aa7a39fbe9e14f75 Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Mon, 2 Oct 2023 12:54:22 +0530 Subject: [PATCH 07/12] improve code --- data_structures/arrays/median_two_array.py | 75 ++++++++++++---------- 1 file changed, 41 insertions(+), 34 deletions(-) diff --git a/data_structures/arrays/median_two_array.py b/data_structures/arrays/median_two_array.py index 4f63e3accf94..8fbef52f0515 100644 --- a/data_structures/arrays/median_two_array.py +++ b/data_structures/arrays/median_two_array.py @@ -1,46 +1,53 @@ -class Solution: - def find_median_sorted_arrays(self, nums1: list[int], nums2: list[int]) -> float: - """ - Find the median of two sorted arrays. +def find_median_sorted_arrays(nums1: list[int], nums2: list[int]) -> float: + """ + Find the median of two arrays. - Args: - nums1: The first sorted array. - nums2: The second sorted array. + Args: + nums1: The first array. + nums2: The second array. - Returns: - The median of the two sorted arrays. + Returns: + The median of the two arrays. - Example: - >>> sol = Solution() - >>> sol.find_median_sorted_arrays([1, 3], [2]) + Examples: + >>> find_median_sorted_arrays([1, 3], [2]) 2.0 - >>> sol.find_median_sorted_arrays([1, 2], [3, 4]) + >>> find_median_sorted_arrays([1, 2], [3, 4]) 2.5 - >>> sol.find_median_sorted_arrays([0, 0], [0, 0]) + >>> find_median_sorted_arrays([0, 0], [0, 0]) 0.0 - """ - # Merge the arrays into a single sorted array. - merged = nums1 + nums2 - - # Sort the merged array. - merged.sort() - - # Calculate the total number of elements in the merged array. - total = len(merged) - - if total % 2 == 1: - # If the total number of elements is odd, return the - # middle element as the median. - return float(merged[total // 2]) - else: - # If the total number of elements is even, calculate - # the average of the two middle elements as the median. - middle1 = merged[total // 2 - 1] - middle2 = merged[total // 2] - return (float(middle1) + float(middle2)) / 2.0 + >>> find_median_sorted_arrays([], []) + Traceback (most recent call last): + ... + ValueError: Both input arrays are empty. + + >>> find_median_sorted_arrays([], [1]) + 1.0 + + >>> find_median_sorted_arrays([-1000], [1000]) + 0.0 + + >>> find_median_sorted_arrays([-1.1, -2.2], [-3.3, -4.4]) + -2.75 + """ + if not nums1 and not nums2: + raise ValueError("Both input arrays are empty.") + + # Merge the arrays into a single sorted array. + merged = sorted(nums1 + nums2) + total = len(merged) + + if total % 2 == 1: # If the total number of elements is odd + return float(merged[total // 2]) # then return the middle element + + # If the total number of elements is even, calculate + # the average of the two middle elements as the median. + middle1 = merged[total // 2 - 1] + middle2 = merged[total // 2] + return (float(middle1) + float(middle2)) / 2.0 if __name__ == "__main__": import doctest From 013d1eaa51ca2837d17b779f6fb3f8431e8b5114 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 2 Oct 2023 07:26:30 +0000 Subject: [PATCH 08/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/median_two_array.py | 1 + 1 file changed, 1 insertion(+) diff --git a/data_structures/arrays/median_two_array.py b/data_structures/arrays/median_two_array.py index 8fbef52f0515..71800c0f9629 100644 --- a/data_structures/arrays/median_two_array.py +++ b/data_structures/arrays/median_two_array.py @@ -49,6 +49,7 @@ def find_median_sorted_arrays(nums1: list[int], nums2: list[int]) -> float: middle2 = merged[total // 2] return (float(middle1) + float(middle2)) / 2.0 + if __name__ == "__main__": import doctest From 4b75fcb3795b2c73f2f267f2733e85b0b5ab6087 Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Mon, 2 Oct 2023 17:28:58 +0530 Subject: [PATCH 09/12] add documentation --- data_structures/arrays/median_two_array.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/data_structures/arrays/median_two_array.py b/data_structures/arrays/median_two_array.py index 71800c0f9629..8db6c1b7378c 100644 --- a/data_structures/arrays/median_two_array.py +++ b/data_structures/arrays/median_two_array.py @@ -1,3 +1,12 @@ +""" +This algorithm for finding the median of two sorted arrays would +typically be used in scenarios where you have two sorted sets +of data (arrays) and need to calculate the middle value or the average +of the middle values to get a representative central value. Common use cases +include statistical analysis and data processing in various fields +such as finance, engineering, and scientific research. +""" + def find_median_sorted_arrays(nums1: list[int], nums2: list[int]) -> float: """ Find the median of two arrays. @@ -49,7 +58,6 @@ def find_median_sorted_arrays(nums1: list[int], nums2: list[int]) -> float: middle2 = merged[total // 2] return (float(middle1) + float(middle2)) / 2.0 - if __name__ == "__main__": import doctest From 3785ca5070d5547f6d7c56b868708470ebe716d4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 2 Oct 2023 12:01:59 +0000 Subject: [PATCH 10/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/median_two_array.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/data_structures/arrays/median_two_array.py b/data_structures/arrays/median_two_array.py index 8db6c1b7378c..12d95f52ff27 100644 --- a/data_structures/arrays/median_two_array.py +++ b/data_structures/arrays/median_two_array.py @@ -1,12 +1,13 @@ """ -This algorithm for finding the median of two sorted arrays would -typically be used in scenarios where you have two sorted sets -of data (arrays) and need to calculate the middle value or the average -of the middle values to get a representative central value. Common use cases -include statistical analysis and data processing in various fields +This algorithm for finding the median of two sorted arrays would +typically be used in scenarios where you have two sorted sets +of data (arrays) and need to calculate the middle value or the average +of the middle values to get a representative central value. Common use cases +include statistical analysis and data processing in various fields such as finance, engineering, and scientific research. """ + def find_median_sorted_arrays(nums1: list[int], nums2: list[int]) -> float: """ Find the median of two arrays. @@ -58,6 +59,7 @@ def find_median_sorted_arrays(nums1: list[int], nums2: list[int]) -> float: middle2 = merged[total // 2] return (float(middle1) + float(middle2)) / 2.0 + if __name__ == "__main__": import doctest From 96feb7711b0f766686817714decdedd8197b8d75 Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Tue, 3 Oct 2023 03:23:55 +0530 Subject: [PATCH 11/12] update --- data_structures/arrays/median_two_array.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/data_structures/arrays/median_two_array.py b/data_structures/arrays/median_two_array.py index 12d95f52ff27..cbe765619ac2 100644 --- a/data_structures/arrays/median_two_array.py +++ b/data_structures/arrays/median_two_array.py @@ -1,13 +1,7 @@ """ -This algorithm for finding the median of two sorted arrays would -typically be used in scenarios where you have two sorted sets -of data (arrays) and need to calculate the middle value or the average -of the middle values to get a representative central value. Common use cases -include statistical analysis and data processing in various fields -such as finance, engineering, and scientific research. +https://www.enjoyalgorithms.com/blog/median-of-two-sorted-arrays """ - def find_median_sorted_arrays(nums1: list[int], nums2: list[int]) -> float: """ Find the median of two arrays. From 1ef8f4b2ea083ea8120dd9e20aa51592309f496b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 2 Oct 2023 21:54:47 +0000 Subject: [PATCH 12/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/median_two_array.py | 1 + 1 file changed, 1 insertion(+) diff --git a/data_structures/arrays/median_two_array.py b/data_structures/arrays/median_two_array.py index cbe765619ac2..972b0ee44201 100644 --- a/data_structures/arrays/median_two_array.py +++ b/data_structures/arrays/median_two_array.py @@ -2,6 +2,7 @@ https://www.enjoyalgorithms.com/blog/median-of-two-sorted-arrays """ + def find_median_sorted_arrays(nums1: list[int], nums2: list[int]) -> float: """ Find the median of two arrays.