-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path33_MS_Running_Median.py
executable file
·229 lines (168 loc) · 5.71 KB
/
33_MS_Running_Median.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
"""
Compute the running median of a sequence of numbers.
That is, given a stream of numbers, print out the median of the list so far on each new element.
Recall that the median of an even-numbered list is the average of the two middle numbers.
For example, given the sequence [2, 1, 5, 7, 2, 0, 5], your algorithm should print out:
2
1.5
2
3.5
2
2
2
"""
class minHeap:
def __init__(self):
self._heap = []
def _has_parent(self, idx):
if (idx-1)/2 < 0:
return False
else:
return True
def _has_left_child(self,idx):
if (idx*2) +1 > self.get_len() -1:
return False
else:
return True
def _has_right_child(self,idx):
if (idx*2) +2 > self.get_len() -1:
return False
else:
return True
def get_parent_idx(self, idx):
return (idx-1)//2
def _get_left_child_idx(self ,idx):
return (idx*2) +1
def _get_right_child_idx(self, idx):
return (idx * 2) + 2
def parent(self,idx):
return self._heap[(idx-1)//2]
def left_child(self, idx):
return self._heap[(idx * 2) + 1]
def right_child(self, idx):
return self._heap[(idx * 2) + 2]
def is_empty(self):
if len(self._heap) == 0:
return True
else:
return False
def get_len(self):
return len(self._heap)
def _swap(self, idxA, idxB):
self._heap[idxA], self._heap[idxB] = self._heap[idxB], self._heap[idxA]
def peak(self):
return self._heap[0]
def pop(self):
item = self._heap[0]
# remove last element and set it to root
self._heap[0] = self._heap[-1]
self._heap = self._heap[:-1]
self._heapifyDown() # make sure everything is in correct order
return item
def add(self, element):
# add element to the end of the list
self._heap.append(element)
self._heapifyUp()
def _heapifyDown(self):
index = 0
while self._has_left_child(index):
smaller_child_idx = self._get_left_child_idx(index)
if self._has_right_child(index) and self.right_child(index) < self.left_child(index):
smaller_child_idx = self._get_right_child_idx(index)
if self._heap[index] < self._heap[smaller_child_idx]:
break # heap in order
else:
self._swap(index, smaller_child_idx)
index = smaller_child_idx
def _heapifyUp(self):
index = len(self._heap) -1
while self._has_parent(index) and self.parent(index) > self._heap[index]:
parent_idx = self.get_parent_idx(index)
self._swap(parent_idx, index )
index = parent_idx
class maxHeap:
def __init__(self):
self._heap = []
def _has_parent(self, idx):
if (idx-1)/2 < 0:
return False
else:
return True
def _has_left_child(self,idx):
if (idx*2) +1 > self.get_len() -1:
return False
else:
return True
def _has_right_child(self,idx):
if (idx*2) +2 > self.get_len() -1:
return False
else:
return True
def get_parent_idx(self, idx):
return (idx-1)//2
def _get_left_child_idx(self ,idx):
return (idx*2) +1
def _get_right_child_idx(self, idx):
return (idx * 2) + 2
def parent(self,idx):
return self._heap[(idx-1)//2]
def left_child(self, idx):
return self._heap[(idx * 2) + 1]
def right_child(self, idx):
return self._heap[(idx * 2) + 2]
def is_empty(self):
if len(self._heap) == 0:
return True
else:
return False
def get_len(self):
return len(self._heap)
def _swap(self, idxA, idxB):
self._heap[idxA], self._heap[idxB] = self._heap[idxB], self._heap[idxA]
def peak(self):
return self._heap[0]
def pop(self):
item = self._heap[0]
# remove last element and set it to root
self._heap[0] = self._heap[-1]
self._heap = self._heap[:-1]
self._heapifyDown() # make sure everything is in correct order
return item
def add(self, element):
# add element to the end of the list
self._heap.append(element)
self._heapifyUp()
def _heapifyDown(self):
index = 0
while self._has_left_child(index):
smaller_child_idx = self._get_left_child_idx(index)
if self._has_right_child(index) and self.right_child(index) > self.left_child(index):
smaller_child_idx = self._get_right_child_idx(index)
if self._heap[index] > self._heap[smaller_child_idx]:
break # heap in order
else:
self._swap(index, smaller_child_idx)
index = smaller_child_idx
def _heapifyUp(self):
index = len(self._heap) -1
while self._has_parent(index) and self.parent(index) < self._heap[index]:
parent_idx = self.get_parent_idx(index)
self._swap(parent_idx, index )
index = parent_idx
def running_median(arr):
min_heap = minHeap()
max_heap = maxHeap()
for element in arr:
min_heap.add(element)
if min_heap.get_len() > max_heap.get_len()+1: # if size diff b/w min_heap > 1
temp = min_heap.pop()
max_heap.add(temp)
if min_heap.get_len() == max_heap.get_len():
# print running median
print((min_heap.peak()+max_heap.peak())/2)
else:
# odd sized list
print(min_heap.peak())
if __name__ == '__main__':
arr = [2, 1, 5, 7, 2, 0, 5]
running_median(arr)