You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.4 KiB

def getMedian(list1, list2):
list1.extend(list2)
for i in range(len(list1) - 1):
minimum = i
for j in range( i + 1, len(list1)):
if(list1[j] < list1[minimum]):
minimum = j
if(minimum != i):
list1[i], list1[minimum] = list1[minimum], list1[i]
if len(list1) % 2 == 0:
index = int(len(list1) / 2)
median = (list1[index] + list1[index-1]) / 2
else:
index = int((len(list1) - 1) / 2)
median = list1[index]
return float(median)
# ↑ ↑ ↑ Your Code ↑ ↑ ↑
tests = 0
passed = 0
def test(nums1, nums2, output):
median = getMedian(nums1, nums2)
print("Array: Nums1: "+str(nums1)+" Nums2: "+str(nums2))
print("Median expected: "+str(output))
print("Median got: "+str(median))
print("Is Median equal: "+str(median == output))
print()
global tests, passed
tests = tests + 1
passed = passed + 1 if median == output else passed + 0
nums1 = [1,3]
nums2 = [2]
output = 2.00000
test(nums1, nums2, output)
nums1 = [1,2]
nums2 = [3,4]
output = 2.50000
test(nums1, nums2, output)
nums1 = [0,0]
nums2 = [0,0]
output = 0.00000
test(nums1, nums2, output)
nums1 = []
nums2 = [1]
output = 1.00000
test(nums1, nums2, output)
nums1 = [2]
nums2 = []
output = 2.00000
test(nums1, nums2, output)
print(str(tests)+" Tests")
print(str(passed)+" Passed/"+str(tests-passed)+" Failed")