-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCourse2_Week3.py
196 lines (166 loc) · 5.91 KB
/
Course2_Week3.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# median maintenance
import random
class MaxBinHeap:
def __init__(self):
self.heapList = [0] #need to initialize with 1 element to shift all elements by 1, to make calculations easier
self.currentSize = 0 #0 above is a dummy element, that's why this is started at 0 not 1
def percUp(self, i):
# iterative version seems more elegant than my recursive one
while i//2 > 0: #while there are still kids
if self.heapList[i] > self.heapList[i//2]: #if kid bigger than parent
self.heapList[i], self.heapList[i//2] = self.heapList[i//2], self.heapList[i] #then swap
i = i//2 #now look at kid's kid
def insert(self, k):
self.heapList.append(k)
self.currentSize += 1
self.percUp(self.currentSize)
def percDown(self, i):
while i*2 <= self.currentSize:
# print(f"i*2 is {i*2}")
mc = self.max_child(i)
if self.heapList[i] < self.heapList[mc]: #if parent smaller than child
self.heapList[i], self.heapList[mc] = self.heapList[mc], self.heapList[i]
i = mc
def max_child(self, i):
if i*2+1 > self.currentSize:
return i * 2
else:
if self.heapList[i*2] > self.heapList[i*2+1]:
return i * 2
else:
return i * 2 + 1
def del_max(self):
# interesting so 0 seems to act like a buffer
retval = self.heapList[1]
self.heapList[1] = self.heapList[self.currentSize]
self.currentSize -= 1
self.heapList.pop()
self.percDown(1)
return retval
def build_heap(self, alist):
i = len(alist) // 2
self.currentSize = len(alist)
self.heapList = [0] + alist[:]
while i > 0:
self.percDown(i)
i -= 1
return self.heapList
# ==============================================================================
# MIN
class MinBinHeap:
def __init__(self):
self.heapList = [0]
self.currentSize = 0
def percUp(self, i):
while i//2 > 0: #while there are still kids
if self.heapList[i] < self.heapList[i//2]: #if kid SMALLER than parent
self.heapList[i], self.heapList[i//2] = self.heapList[i//2], self.heapList[i] #then swap
i = i//2 #now look at kid's kid
def insert(self, k):
self.heapList.append(k)
self.currentSize += 1
self.percUp(self.currentSize)
def percDown(self, i):
while i*2 <= self.currentSize:
mc = self.min_child(i)
if self.heapList[i] > self.heapList[mc]: #if parent BIGGER than child
self.heapList[i], self.heapList[mc] = self.heapList[mc], self.heapList[i]
i = mc
def min_child(self, i):
if i*2+1 > self.currentSize:
return i * 2
else:
if self.heapList[i*2] < self.heapList[i*2+1]:
return i * 2
else:
return i * 2 + 1
def del_min(self):
retval = self.heapList[1]
self.heapList[1] = self.heapList[self.currentSize]
self.currentSize -= 1
self.heapList.pop()
self.percDown(1)
return retval
def build_heap(self, alist):
i = len(alist) // 2
self.currentSize = len(alist)
self.heapList = [0] + alist[:]
while i > 0:
self.percDown(i)
i -= 1
return self.heapList
# ==============================================================================
def calc_median(L):
"""Median maintenance algo.
Implemented as per lecture slides. See algo1slides / Part 12."""
h_low = MaxBinHeap() #supports extract max
h_high = MinBinHeap() #supports extract min
medians = []
j = 1
for i in L:
len_low = len(h_low.heapList)
len_high = len(h_high.heapList)
# starting condition
if len_low == 1 and len_high == 1:
h_low.insert(i)
m_low = h_low.heapList[1]
elif len_high == 1:
if i < m_low:
h_low.insert(i)
m_low = h_low.heapList[1]
else:
h_high.insert(i)
m_high = h_high.heapList[1]
else:
# if bigger put into high heap
if i > m_low:
h_high.insert(i)
m_high = h_high.heapList[1]
# if smaller put into small heap
elif i < m_high:
h_low.insert(i)
m_low = h_low.heapList[1]
# if in between put into either one
else:
h_low.insert(i) #picked whichever
m_low = h_low.heapList[1]
# if heaps differ by 2, fix imbalance
if abs(len(h_low.heapList) - len(h_high.heapList)) > 1:
if len(h_low.heapList) > len(h_high.heapList):
e = h_low.del_max()
h_high.insert(e)
else:
e = h_high.del_min()
h_low.insert(e)
m_high = h_high.heapList[1]
m_low = h_low.heapList[1]
if j % 2 == 0:
median = h_low.heapList[1]
else:
if len(h_low.heapList) > len(h_high.heapList):
median = h_low.heapList[1]
else:
median = h_high.heapList[1]
medians.append(median)
# print('-'*20)
# print(h_low.heapList)
# print(h_high.heapList)
# print(f"median on the {j} round is {median}")
# print(len(h_low.heapList))
# print(len(h_high.heapList))
j += 1
# if j % 100 == 0:
# print(j)
total = sum(medians)
print(total)
mod_total = total % 10000
print(mod_total)
random.seed(1)
L = [random.randint(1,100) for _ in range(10)]
# calc_median(L)
with open("median.txt") as f:
txt = f.readlines()
txt = [int(t.strip('\n')) for t in txt]
# print(txt)
# print(len(txt))
calc_median(txt)