Skip to content

Commit e7dc1f2

Browse files
author
Partho Biswas
committed
no message
1 parent c189166 commit e7dc1f2

File tree

1 file changed

+10
-29
lines changed

1 file changed

+10
-29
lines changed

algoexpert.io/python/Continuous_Median.py

+10-29
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
1-
2-
31
class ContinuousMedianHandler:
42
def __init__(self):
53
self.lowers = Heap(MAX_HEAP_FUNC, [])
64
self.greaters = Heap(MIN_HEAP_FUNC, [])
75
self.median = None
86

9-
10-
def insert(self, number):
7+
def insert(self, number):
118
if not self.lowers.length or number < self.lowers.peek():
129
self.lowers.insert(number)
1310
else:
1411
self.greaters.insert(number)
1512
self.rebalannceHeap()
1613
self.updateMedian()
1714

18-
1915
def rebalannceHeap(self):
2016
if self.lowers.length - self.greaters.length == 2:
2117
self.greaters.insert(self.lowers.remove())
2218
elif self.greaters.length - self.lowers.length == 2:
2319
self.lowers.insert(self.greaters.remove())
2420

25-
2621
def updateMedian(self):
2722
if self.lowers.length == self.greaters.length:
2823
self.median = (self.lowers.peek() + self.greaters.peek()) / 2
@@ -31,32 +26,26 @@ def updateMedian(self):
3126
else:
3227
self.median = self.greaters.peek()
3328

34-
3529
def getMedian(self):
3630
return self.median
3731

3832

39-
40-
41-
4233
class Heap:
4334
def __init__(self, comparisonFunc, array):
4435
self.heap = self.buildHeap(array)
4536
self.comparisonFunc = comparisonFunc
46-
self.lenght = len(self.heap)
47-
37+
self.length = len(self.heap)
4838

4939
def buildHeap(self, array):
5040
firstParentIdx = (len(array) - 2) // 2
5141
for currentIdx in reversed(range(firstParentIdx + 1)):
5242
self.shiftDown(currentIdx, len(array) - 1, array)
5343
return array
5444

55-
5645
def shiftDown(self, currentIdx, endIdx, heap):
5746
childOneIdx = currentIdx * 2 + 1
5847
while childOneIdx <= endIdx:
59-
childTwoIdx = currentIdx * 2 + 2 if currentIdx * 2 + 2 <= endIdx else -1
48+
childTwoIdx = currentIdx * 2 + 2 if currentIdx * 2 + 2 <= endIdx else -1
6049
if childTwoIdx != -1:
6150
if self.comparisonFunc(heap[childTwoIdx], heap[childOneIdx]):
6251
idxToSwap = childTwoIdx
@@ -71,9 +60,8 @@ def shiftDown(self, currentIdx, endIdx, heap):
7160
else:
7261
return
7362

74-
7563
def shiftUp(self, currentIdx, heap):
76-
parentIdx = (currentIdx - 1) // 2
64+
parentIdx = (currentIdx - 1) // 2
7765
while currentIdx > 0:
7866
if self.comparisonFunc(heap[currentIdx], heap[parentIdx]):
7967
self.swap(currentIdx, parentIdx, heap)
@@ -82,32 +70,25 @@ def shiftUp(self, currentIdx, heap):
8270
else:
8371
return
8472

85-
8673
def peek(self):
8774
return self.heap[0]
8875

89-
9076
def remove(self):
91-
self.swap(0, self.lenght - 1, self.heap)
77+
self.swap(0, self.length - 1, self.heap)
9278
valueToRemove = self.heap.pop()
93-
self.lenght -= 1
94-
self.shiftDown(0, self.lenght - 1, self.heap)
79+
self.length -= 1
80+
self.shiftDown(0, self.length - 1, self.heap)
9581
return valueToRemove
9682

97-
98-
def innsert(self, value):
83+
def insert(self, value):
9984
self.heap.append(value)
100-
self.lenght += 1
101-
self.shiftUp(self.lenght - 1, self.heap)
102-
85+
self.length += 1
86+
self.shiftUp(self.length - 1, self.heap)
10387

10488
def swap(self, i, j, array):
10589
array[i], array[j] = array[j], array[i]
10690

10791

108-
109-
110-
11192
def MAX_HEAP_FUNC(a, b):
11293
return a > b
11394

0 commit comments

Comments
 (0)