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 1 commit
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
25 changes: 12 additions & 13 deletions other/median_of_two_arrays.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# This code finds the median of two arrays, even if they are not sorted initially
def find_median_arrays(nums1, nums2):
list3 = nums1 + nums2
list3 = nums1 + nums2
list3.sort()
if divmod(len(list3), 2).[1] == 1:
a = int(len(list3) / 2)
return list3[a]
all_numbers = nums1 + nums2
all_numbers.sort()
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
all_numbers = nums1 + nums2
all_numbers.sort()
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(len(list3) / 2)
return (list3[a] + list3[a - 1]) / 2
a = int(div)
return (all_numbers[a] + all_numbers[a - 1]) / 2


if __name__ == "__main__":
n1 = [int(x) for x in input("Enter the elements of first array: ").split()]
n2 = [int(x) for x in input("Enter the elements of second array: ").split()]
m = find_median_arrays(n1, n2)
print(f'The median of two arrays is: {m}')
array_1 = [int(x) for x in input("Enter the elements of first array: ").split()]
array_2 = [int(x) for x in input("Enter the elements of second array: ").split()]
print(f'The median of two arrays is: {find_median_arrays(array_1, array_2)}')