-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Median of Two Arrays #3554
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
Merged
Median of Two Arrays #3554
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
8db9cef
Create medianOf TwoArrays.py
anneCoder1805 2825eb0
Rename medianOf TwoArrays.py to median_of _two_arrays.py
anneCoder1805 4bf5e03
Rename median_of _two_arrays.py to median_of_two_arrays.py
anneCoder1805 e7f8d0e
Update median_of_two_arrays.py
anneCoder1805 28dfa86
Update median_of_two_arrays.py
anneCoder1805 ea300c1
Update median_of_two_arrays.py
anneCoder1805 93e0e8a
Update median_of_two_arrays.py
anneCoder1805 14b43b5
Update median_of_two_arrays.py
anneCoder1805 32a8193
Update median_of_two_arrays.py
anneCoder1805 45fb6da
Update median_of_two_arrays.py
anneCoder1805 2578a16
Update median_of_two_arrays.py
anneCoder1805 6510be6
Update median_of_two_arrays.py
anneCoder1805 472040c
Update median_of_two_arrays.py
anneCoder1805 c608218
Update median_of_two_arrays.py
anneCoder1805 6b5d719
Update median_of_two_arrays.py
anneCoder1805 f8e7b76
Update median_of_two_arrays.py
anneCoder1805 42b4591
Update median_of_two_arrays.py
anneCoder1805 320b42f
Update median_of_two_arrays.py
anneCoder1805 8e74578
Update median_of_two_arrays.py
anneCoder1805 1a1fa28
Update median_of_two_arrays.py
anneCoder1805 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,35 @@ | ||
from typing import List | ||
|
||
|
||
def median_of_two_arrays(nums1: List[float], nums2: List[float]) -> float: | ||
""" | ||
>>> median_of_two_arrays([1, 2], [3]) | ||
2 | ||
>>> median_of_two_arrays([0, -1.1], [2.5, 1]) | ||
0.5 | ||
>>> median_of_two_arrays([], [2.5, 1]) | ||
1.75 | ||
>>> median_of_two_arrays([], [0]) | ||
0 | ||
>>> median_of_two_arrays([], []) | ||
Traceback (most recent call last): | ||
... | ||
IndexError: list index out of range | ||
""" | ||
all_numbers = sorted(nums1 + nums2) | ||
div, mod = divmod(len(all_numbers), 2) | ||
if mod == 1: | ||
a = int(len(all_numbers) / 2) | ||
return all_numbers[a] | ||
else: | ||
a = int(div) | ||
return (all_numbers[a] + all_numbers[a - 1]) / 2 | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
array_1: List[float] = [float(x) for x in input("Enter the elements of first array: ").split()] | ||
array_2: List[float] = [float(x) for x in input("Enter the elements of second array: ").split()] | ||
print(f"The median of two arrays is: {median_of_two_arrays(array_1, array_2)}") | ||
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.