-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
add median of two sorted array #9386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cclauss
merged 12 commits into
TheAlgorithms:master
from
BamaCharanChhandogi:add/median-two-array
Oct 4, 2023
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9d47c87
add median of two sorted array
BamaCharanChhandogi 8981a8e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ab0ecd4
fix syntax
BamaCharanChhandogi fdc5914
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8077618
fix syntax
BamaCharanChhandogi 97e335d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2bad9c1
improve code
BamaCharanChhandogi 013d1ea
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4b75fcb
add documentation
BamaCharanChhandogi 3785ca5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 96feb77
update
BamaCharanChhandogi 1ef8f4b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
""" | ||
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. | ||
|
||
Args: | ||
nums1: The first array. | ||
nums2: The second array. | ||
|
||
Returns: | ||
The median of the two arrays. | ||
|
||
Examples: | ||
>>> find_median_sorted_arrays([1, 3], [2]) | ||
2.0 | ||
|
||
>>> find_median_sorted_arrays([1, 2], [3, 4]) | ||
2.5 | ||
|
||
>>> find_median_sorted_arrays([0, 0], [0, 0]) | ||
0.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 | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.