-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment6B.py
More file actions
49 lines (40 loc) · 1013 Bytes
/
Assignment6B.py
File metadata and controls
49 lines (40 loc) · 1013 Bytes
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
38
39
40
41
42
43
44
45
46
47
48
49
__author__ = 'zihaozhu'
import heapq
heap_low=None
heap_high=None
def heap_init():
global heap_low
global heap_high
heap_low = []
heap_high = []
def heap_insert(x):
global heap_low
global heap_high
if(len(heap_low)==0):
heapq.heappush(heap_low,-x)
else:
m = -heap_low[0]
if(x>m):
heapq.heappush(heap_high,x)
if(len(heap_high)>len(heap_low)):
y = heapq.heappop(heap_high)
heapq.heappush(heap_low,y)
else:
heapq.heappush(heap_low,-x)
if(len(heap_low)>len(heap_high)):
y = -heapq.heappop(heap_low)
heapq.heappush(heap_high,y)
return -heap_low[0]
fh = open("median.txt","r")
lines = open('Median.txt').read().splitlines()
data = map(lambda x: int(x), lines)
medians = []
heap_init()
for x in data:
median = heap_insert(x)
medians.append(median)
sum=0
for med in medians:
sum= sum+med%10000
sum = sum%10000
print(sum)