Skip to content

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 20 commits into from
Oct 20, 2020
Merged
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions other/median_of_two_arrays.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This code finds the median of two arrays, even if they are not sorted initially
def findMedianArrays(nums1, nums2):
list3 = nums1+nums2
list3 = nums1 + nums2
list3.sort()
if len(list3)%2==1:
a = int(len(list3)/2)
if len(list3) % 2 == 1:
a = int(len(list3) / 2)
return list3[a]
else:
a = int(len(list3)/2)
return (list3[a]+list3[a-1])/2
a = int(len(list3) / 2)
return (list3[a] + list3[a - 1]) / 2


def main():
from doctest import testmod

testmod()
n1 = list(map(int, input('Enter elements of an array: ').split()))
n2 = list(map(int, input('Enter elements of another array: ').split()))
print('The median of two arrays is: ',findMedianArrays(n1,n2))
n1 = list(map(int, input("Enter elements of an array: ").split()))
n2 = list(map(int, input("Enter elements of another array: ").split()))
print("The median of two arrays is: ", findMedianArrays(n1, n2))


if __name__ == "__main__":
main()