-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
38 lines (33 loc) · 1.07 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Enter your code here. Read input from STDIN. Print output to STDOUT
def get_midpoint(arr):
return int(len(arr) / 2)
def find_median(arr):
if len(arr) % 2 != 0:
mid_point = get_midpoint(arr)
return round(arr[mid_point], 1)
else:
top_middle = (len(arr) / 2)
bottom_middle = (len(arr) / 2) - 1
return round((arr[top_middle] + arr[bottom_middle]) / 2, 1)
def get_halves(arr):
halves = []
midpoint = get_midpoint(arr)
if len(arr) % 2 != 0:
arr.pop(midpoint)
lower_half = arr[:midpoint]
upper_half = arr[midpoint:]
halves.append(lower_half)
halves.append(upper_half)
return halves
if __name__ == '__main__':
_ = input()
arr = [int(x) for x in input().split(' ')]
freq = [int(x) for x in input().split(' ')]
new_arr = [[x] * y for x, y in zip(arr, freq)]
new_arr.sort()
flat_arr = [x for sub_arr in new_arr for x in sub_arr]
print(flat_arr)
lower_half, upper_half = get_halves(flat_arr)
q1 = find_median(lower_half)
q3 = find_median(upper_half)
print((q3 - q1))