From 8db9cef4280df49ba28144acc80a78d1b0423dfa Mon Sep 17 00:00:00 2001 From: anneCoder1805 <66819522+anneCoder1805@users.noreply.github.com> Date: Mon, 19 Oct 2020 16:55:52 +0530 Subject: [PATCH 01/20] Create medianOf TwoArrays.py This code finds the median of two arrays (which may or may not be sorted initially). Example: Enter elements of an array: 1 5 4 2 Enter elements of another array: 1 7 4 2 7 The median of two arrays is : 4 --- other/medianOf TwoArrays.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 other/medianOf TwoArrays.py diff --git a/other/medianOf TwoArrays.py b/other/medianOf TwoArrays.py new file mode 100644 index 000000000000..e2bc67909c93 --- /dev/null +++ b/other/medianOf TwoArrays.py @@ -0,0 +1,15 @@ +# This code finds the median of two arrays, even if they are not sorted initially +def findMedianArrays(nums1, nums2): + list3 = nums1+nums2 + list3.sort() + 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 + +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())) + print('The median of two arrays is: ', findMedianArrays(n1,n2)) From 2825eb093765e20d680902e8cf84b14e69a76cdb Mon Sep 17 00:00:00 2001 From: anneCoder1805 <66819522+anneCoder1805@users.noreply.github.com> Date: Mon, 19 Oct 2020 16:58:30 +0530 Subject: [PATCH 02/20] Rename medianOf TwoArrays.py to median_of _two_arrays.py --- other/{medianOf TwoArrays.py => median_of _two_arrays.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename other/{medianOf TwoArrays.py => median_of _two_arrays.py} (100%) diff --git a/other/medianOf TwoArrays.py b/other/median_of _two_arrays.py similarity index 100% rename from other/medianOf TwoArrays.py rename to other/median_of _two_arrays.py From 4bf5e03966229c6d4c03da2d4109826ec8f2cbf9 Mon Sep 17 00:00:00 2001 From: anneCoder1805 <66819522+anneCoder1805@users.noreply.github.com> Date: Mon, 19 Oct 2020 17:19:38 +0530 Subject: [PATCH 03/20] Rename median_of _two_arrays.py to median_of_two_arrays.py --- other/{median_of _two_arrays.py => median_of_two_arrays.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename other/{median_of _two_arrays.py => median_of_two_arrays.py} (100%) diff --git a/other/median_of _two_arrays.py b/other/median_of_two_arrays.py similarity index 100% rename from other/median_of _two_arrays.py rename to other/median_of_two_arrays.py From e7f8d0e47dd37c578d8b866dc602e16bc8773921 Mon Sep 17 00:00:00 2001 From: anneCoder1805 <66819522+anneCoder1805@users.noreply.github.com> Date: Mon, 19 Oct 2020 17:23:50 +0530 Subject: [PATCH 04/20] Update median_of_two_arrays.py --- other/median_of_two_arrays.py | 1 - 1 file changed, 1 deletion(-) diff --git a/other/median_of_two_arrays.py b/other/median_of_two_arrays.py index e2bc67909c93..cffbe5209f42 100644 --- a/other/median_of_two_arrays.py +++ b/other/median_of_two_arrays.py @@ -8,7 +8,6 @@ def findMedianArrays(nums1, nums2): else: 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())) From 28dfa867234f2c052f1f53bda509a0ad2732ac34 Mon Sep 17 00:00:00 2001 From: anneCoder1805 <66819522+anneCoder1805@users.noreply.github.com> Date: Tue, 20 Oct 2020 11:46:52 +0530 Subject: [PATCH 05/20] Update median_of_two_arrays.py --- other/median_of_two_arrays.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/other/median_of_two_arrays.py b/other/median_of_two_arrays.py index cffbe5209f42..d070378e26dd 100644 --- a/other/median_of_two_arrays.py +++ b/other/median_of_two_arrays.py @@ -7,8 +7,8 @@ def findMedianArrays(nums1, nums2): return list3[a] else: a = int(len(list3)/2) - return (list3[a] + list3[a - 1])/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())) - print('The median of two arrays is: ', findMedianArrays(n1,n2)) + print('The median of two arrays is: ',findMedianArrays(n1,n2)) From ea300c168e465b87f8bc549851e02b2ecdaeb514 Mon Sep 17 00:00:00 2001 From: anneCoder1805 <66819522+anneCoder1805@users.noreply.github.com> Date: Tue, 20 Oct 2020 11:59:54 +0530 Subject: [PATCH 06/20] Update median_of_two_arrays.py --- other/median_of_two_arrays.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/other/median_of_two_arrays.py b/other/median_of_two_arrays.py index d070378e26dd..aba70cce5315 100644 --- a/other/median_of_two_arrays.py +++ b/other/median_of_two_arrays.py @@ -8,7 +8,11 @@ def findMedianArrays(nums1, nums2): else: a = int(len(list3)/2) return (list3[a]+list3[a-1])/2 -if __name__ == "__main__": +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)) +if __name__ == "__main__": + main() From 93e0e8aa6c511f5f1396033224f21040449dec2c Mon Sep 17 00:00:00 2001 From: anneCoder1805 <66819522+anneCoder1805@users.noreply.github.com> Date: Tue, 20 Oct 2020 12:06:07 +0530 Subject: [PATCH 07/20] Update median_of_two_arrays.py --- other/median_of_two_arrays.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/other/median_of_two_arrays.py b/other/median_of_two_arrays.py index aba70cce5315..93ac14ce932f 100644 --- a/other/median_of_two_arrays.py +++ b/other/median_of_two_arrays.py @@ -1,18 +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())) + 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() From 14b43b5d6fe736265bde720ffc9a772b3bae36c8 Mon Sep 17 00:00:00 2001 From: anneCoder1805 <66819522+anneCoder1805@users.noreply.github.com> Date: Tue, 20 Oct 2020 12:13:37 +0530 Subject: [PATCH 08/20] Update median_of_two_arrays.py --- other/median_of_two_arrays.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/other/median_of_two_arrays.py b/other/median_of_two_arrays.py index 93ac14ce932f..131276bf41a1 100644 --- a/other/median_of_two_arrays.py +++ b/other/median_of_two_arrays.py @@ -16,10 +16,10 @@ def findMedianArrays(nums1, nums2): def main(): - from doctest import testmod + from doctest import testmod - testmod() - n1 = list(map(int, input('Enter elements of an array: ').split())) + 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())) From 32a819383f4803ee5398af3f81d7ec6ec2f8e7c3 Mon Sep 17 00:00:00 2001 From: anneCoder1805 <66819522+anneCoder1805@users.noreply.github.com> Date: Tue, 20 Oct 2020 12:23:53 +0530 Subject: [PATCH 09/20] Update median_of_two_arrays.py --- other/median_of_two_arrays.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/other/median_of_two_arrays.py b/other/median_of_two_arrays.py index 131276bf41a1..a61f4903dae7 100644 --- a/other/median_of_two_arrays.py +++ b/other/median_of_two_arrays.py @@ -1,16 +1,16 @@ # 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 = nums1 + nums2 list3.sort() - if len(list3)%2==1: - a = int(len(list3)/2) + 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 a = int(len(list3) / 2) return (list3[a] + list3[a - 1]) / 2 @@ -21,7 +21,7 @@ def main(): 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)) + 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)) @@ -29,3 +29,4 @@ def main(): if __name__ == "__main__": main() + From 45fb6dacfc3370f98490478b7e304ebd88edd1d5 Mon Sep 17 00:00:00 2001 From: anneCoder1805 <66819522+anneCoder1805@users.noreply.github.com> Date: Tue, 20 Oct 2020 12:32:05 +0530 Subject: [PATCH 10/20] Update median_of_two_arrays.py --- other/median_of_two_arrays.py | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/other/median_of_two_arrays.py b/other/median_of_two_arrays.py index a61f4903dae7..d46e28bc45cb 100644 --- a/other/median_of_two_arrays.py +++ b/other/median_of_two_arrays.py @@ -15,18 +15,8 @@ def findMedianArrays(nums1, nums2): return (list3[a] + list3[a - 1]) / 2 -def main(): - from doctest import testmod - - testmod() +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())) - 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() - + m = findMedianArrays(n1, n2) + print(f'The median of two arrays is: {m}') From 2578a16f4d458a976bf4d54bacb2f2e307cf878d Mon Sep 17 00:00:00 2001 From: anneCoder1805 <66819522+anneCoder1805@users.noreply.github.com> Date: Tue, 20 Oct 2020 12:42:23 +0530 Subject: [PATCH 11/20] Update median_of_two_arrays.py --- other/median_of_two_arrays.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/other/median_of_two_arrays.py b/other/median_of_two_arrays.py index d46e28bc45cb..9d4e18fc1fec 100644 --- a/other/median_of_two_arrays.py +++ b/other/median_of_two_arrays.py @@ -1,22 +1,19 @@ # This code finds the median of two arrays, even if they are not sorted initially -def findMedianArrays(nums1, nums2): +def find_median_arrays(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: + if divmod(len(list3), 2).[1] == 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) + n1 = [int(x) for x in input("Enter multiple value: ").split()] + n2 = [int(x) for x in input("Enter multiple value: ").split()] + m = find_media_arrays(n1, n2) print(f'The median of two arrays is: {m}') From 6510be63a2125f5a3b1fd7d8d9657bc77da8bc98 Mon Sep 17 00:00:00 2001 From: anneCoder1805 <66819522+anneCoder1805@users.noreply.github.com> Date: Tue, 20 Oct 2020 12:45:54 +0530 Subject: [PATCH 12/20] Update median_of_two_arrays.py --- other/median_of_two_arrays.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/other/median_of_two_arrays.py b/other/median_of_two_arrays.py index 9d4e18fc1fec..aa777d104242 100644 --- a/other/median_of_two_arrays.py +++ b/other/median_of_two_arrays.py @@ -11,9 +11,8 @@ def find_median_arrays(nums1, nums2): return (list3[a] + list3[a - 1]) / 2 - if __name__ == "__main__": - n1 = [int(x) for x in input("Enter multiple value: ").split()] - n2 = [int(x) for x in input("Enter multiple value: ").split()] + 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_media_arrays(n1, n2) print(f'The median of two arrays is: {m}') From 472040c1a5e7329a7717d168cd11537695f06ea7 Mon Sep 17 00:00:00 2001 From: anneCoder1805 <66819522+anneCoder1805@users.noreply.github.com> Date: Tue, 20 Oct 2020 12:51:37 +0530 Subject: [PATCH 13/20] Update median_of_two_arrays.py --- other/median_of_two_arrays.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/other/median_of_two_arrays.py b/other/median_of_two_arrays.py index aa777d104242..3dd334a51178 100644 --- a/other/median_of_two_arrays.py +++ b/other/median_of_two_arrays.py @@ -8,11 +8,11 @@ def find_median_arrays(nums1, nums2): return list3[a] else: a = int(len(list3) / 2) - return (list3[a] + list3[a - 1]) / 2 + return (list3[a] + list3[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_media_arrays(n1, n2) + m = find_median_arrays(n1, n2) print(f'The median of two arrays is: {m}') From c608218c1ca16490ea34618170b63db756318556 Mon Sep 17 00:00:00 2001 From: anneCoder1805 <66819522+anneCoder1805@users.noreply.github.com> Date: Tue, 20 Oct 2020 13:34:30 +0530 Subject: [PATCH 14/20] Update median_of_two_arrays.py --- other/median_of_two_arrays.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/other/median_of_two_arrays.py b/other/median_of_two_arrays.py index 3dd334a51178..3f15e0a37808 100644 --- a/other/median_of_two_arrays.py +++ b/other/median_of_two_arrays.py @@ -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() + 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)}') From 6b5d7194e4dde141d5a1364934c67015c46b1c15 Mon Sep 17 00:00:00 2001 From: anneCoder1805 <66819522+anneCoder1805@users.noreply.github.com> Date: Tue, 20 Oct 2020 13:38:35 +0530 Subject: [PATCH 15/20] Update median_of_two_arrays.py --- other/median_of_two_arrays.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/other/median_of_two_arrays.py b/other/median_of_two_arrays.py index 3f15e0a37808..8e9044ee15e1 100644 --- a/other/median_of_two_arrays.py +++ b/other/median_of_two_arrays.py @@ -13,5 +13,5 @@ def find_median_arrays(nums1, nums2): if __name__ == "__main__": 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)}') + 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)}') From f8e7b768199110ffd6a268ccfa95953e787a3090 Mon Sep 17 00:00:00 2001 From: anneCoder1805 <66819522+anneCoder1805@users.noreply.github.com> Date: Tue, 20 Oct 2020 13:47:26 +0530 Subject: [PATCH 16/20] Update median_of_two_arrays.py --- other/median_of_two_arrays.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/median_of_two_arrays.py b/other/median_of_two_arrays.py index 8e9044ee15e1..442a74036acb 100644 --- a/other/median_of_two_arrays.py +++ b/other/median_of_two_arrays.py @@ -8,7 +8,7 @@ def find_median_arrays(nums1, nums2): return all_numbers[a] else: a = int(div) - return (all_numbers[a] + all_numbers[a - 1]) / 2 + return (all_numbers[a] + all_numbers[a - 1]) / 2 if __name__ == "__main__": From 42b4591e4dfd0a0a5a078feab4cec64462761fb4 Mon Sep 17 00:00:00 2001 From: anneCoder1805 <66819522+anneCoder1805@users.noreply.github.com> Date: Tue, 20 Oct 2020 13:51:23 +0530 Subject: [PATCH 17/20] Update median_of_two_arrays.py --- other/median_of_two_arrays.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/median_of_two_arrays.py b/other/median_of_two_arrays.py index 442a74036acb..258085c30e0b 100644 --- a/other/median_of_two_arrays.py +++ b/other/median_of_two_arrays.py @@ -14,4 +14,4 @@ def find_median_arrays(nums1, nums2): if __name__ == "__main__": 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)}') + print(f"The median of two arrays is: {find_median_arrays(array_1, array_2)}") From 320b42f1491652a3ceaa8aeb0f08bd7ce4fbf42c Mon Sep 17 00:00:00 2001 From: anneCoder1805 <66819522+anneCoder1805@users.noreply.github.com> Date: Tue, 20 Oct 2020 14:50:39 +0530 Subject: [PATCH 18/20] Update median_of_two_arrays.py --- other/median_of_two_arrays.py | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/other/median_of_two_arrays.py b/other/median_of_two_arrays.py index 258085c30e0b..7ac1eacbe112 100644 --- a/other/median_of_two_arrays.py +++ b/other/median_of_two_arrays.py @@ -1,7 +1,22 @@ -# This code finds the median of two arrays, even if they are not sorted initially -def find_median_arrays(nums1, nums2): - all_numbers = nums1 + nums2 - all_numbers.sort() +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) @@ -12,6 +27,9 @@ def find_median_arrays(nums1, nums2): if __name__ == "__main__": - 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)}") + 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)}") From 8e745782c4d593c925e9c8f5641644ebb8efb9ef Mon Sep 17 00:00:00 2001 From: anneCoder1805 <66819522+anneCoder1805@users.noreply.github.com> Date: Tue, 20 Oct 2020 14:55:28 +0530 Subject: [PATCH 19/20] Update median_of_two_arrays.py --- other/median_of_two_arrays.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/other/median_of_two_arrays.py b/other/median_of_two_arrays.py index 7ac1eacbe112..8d149fa7e7e6 100644 --- a/other/median_of_two_arrays.py +++ b/other/median_of_two_arrays.py @@ -30,6 +30,10 @@ def median_of_two_arrays(nums1: List[float], nums2: List[float]) -> float: 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()] + 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)}") From 1a1fa28899a251010e689da15f599bb0ded89117 Mon Sep 17 00:00:00 2001 From: anneCoder1805 <66819522+anneCoder1805@users.noreply.github.com> Date: Tue, 20 Oct 2020 15:59:47 +0530 Subject: [PATCH 20/20] Update median_of_two_arrays.py --- other/median_of_two_arrays.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/other/median_of_two_arrays.py b/other/median_of_two_arrays.py index 8d149fa7e7e6..cde12f5d7e3b 100644 --- a/other/median_of_two_arrays.py +++ b/other/median_of_two_arrays.py @@ -19,21 +19,15 @@ def median_of_two_arrays(nums1: List[float], nums2: List[float]) -> float: 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] + return all_numbers[div] else: - a = int(div) - return (all_numbers[a] + all_numbers[a - 1]) / 2 + return (all_numbers[div] + all_numbers[div - 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() - ] + array_1 = [float(x) for x in input("Enter the elements of first array: ").split()] + array_2 = [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)}")