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 10 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
22 changes: 22 additions & 0 deletions other/median_of_two_arrays.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
list3 = nums1 + nums2
list3 = nums1 + nums2
all_numbers = nums1 + nums2

Remove duplication and use a more self-documenting variable name.

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


if __name__ == "__main__":
n1 = list(map(int, input('Enter elements of an array: ').split()))
n2 = list(map(int, input('Enter elements of another array: ').split()))
m = findMedianArrays(n1, n2)
print(f'The median of two arrays is: {m}')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
m = findMedianArrays(n1, n2)
print(f'The median of two arrays is: {m}')
print(f'The median of two arrays is: {findMedianArrays(n1, n2)}')