-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathm_golden_mean.py
35 lines (31 loc) · 1.01 KB
/
m_golden_mean.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
def median(a):
half, remainder = divmod(len(a), 2)
if remainder != 0 or a[half - 1] == a[half]:
return a[half]
avg, remainder = divmod(a[half - 1] + a[half], 2)
return avg if not remainder else avg + 0.5
def median_two(a, b):
a_left, a_right = 0, len(a) - 1
b_left, b_right = 0, len(b) - 1
while a_left <= a_right and b_left <= b_right:
if a_left == a_right and b_left == b_right:
avg, remainder = divmod(a[a_left] + b[b_left], 2)
return avg if not remainder else avg + 0.5
if a[a_left] <= b[b_left]:
a_left += 1
else:
b_left += 1
if a[a_right] >= b[b_right]:
a_right -= 1
else:
b_right -= 1
return median(
a[a_left : a_right + 1]
if a_left <= a_right
else b[b_left : b_right + 1]
)
if __name__ == '__main__':
_, _ = input(), input()
a = list(map(int, input().split()))
b = list(map(int, input().split()))
print(median_two(a, b))