Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

How to efficiently create a median finder for a stream of values, in Python?

"""
The median is the middle value of an ordered list 
of elements. If the elements count is even, then
the median is the average of the two middle elements.

For instance, in [3, 4, 5], median = 4, while
in [3, 4], median = (3+4)/2 = 3.5.

The below implementation creates a MedianFinder
class that streamlines the process of finding the median
for a stream of n values. 

Space complexity: O(n), to hold the values in heaps.
"""
from heapq import heappop, heappush

class median_finder:
    # Time complexity: O(1)
    def __init__(self):
        self.los = []
        self.his = []
        self.median = 0
    # Time complexity: O(log2(n))
    def add_num(self, num):
        if len(self.los) == 0 or (-1*self.los[0]) > num:
            heappush(self.los, -1*num)
        else:
            heappush(self.his, num)
        self.rebalance_his_los()
        self.update_median()
    # Time complexity: O(log2(n))
    def rebalance_his_los(self):
        los_length = len(self.los)
        his_length = len(self.his)
        if los_length - his_length == 2:
            heappush(self.his, -1*heappop(self.los))
        elif his_length - los_length == 2:
            heappush(self.los, -1*heappop(self.his))
    # Time complexity: O(1)
    def update_median(self):
        los_length = len(self.los)
        his_length = len(self.his)
        if los_length == his_length:
            self.median = (-1*self.los[0] + self.his[0])/2
        elif los_length > his_length:
            self.median = -1*self.los[0]
        else:
            self.median = self.his[0]
    # Time complexity: O(1)
    def find_median(self):
        return self.median

median_finder_obj1 = median_finder()
median_finder_obj1.add_num(3)
median_finder_obj1.add_num(4)
median_finder_obj1.add_num(5)
print("Median:", median_finder_obj1.find_median())  # Median: 4
median_finder_obj2 = median_finder()
median_finder_obj2.add_num(3)
median_finder_obj2.add_num(4)
print("Median:", median_finder_obj2.find_median())  # Median: 3.5
Comment

PREVIOUS NEXT
Code Example
Python :: how to get total number of rows in listbox tkinter 
Python :: how to python hack 2021 course 
Python :: get package share vs Find Package Share 
Python :: anova in python 
Python :: Can only use .str accessor with string values! 
Python :: install python 3.6 ubuntu 16.04 
Python :: how to know how much lines a file has using python 
Python :: python find all positions of element in list 
Python :: pydotprint 
Python :: open mat file in python 
Python :: pygame tetris game tutorial 
Python :: convert file to base64 python 
Python :: streamlit button to load a file 
Python :: json post python with headers 
Python :: count number of rows pandas condition 
Python :: check if a value in dataframe is nan 
Python :: How to count occurences of a certain item in a numpy array 
Python :: txt file duplicate line remover python 
Python :: converting bool to 1 if it has true and if it is false print 1 
Python :: matplotlib Savefig cuts off title 
Python :: python multiply all elements in array by constant 
Python :: python read word document 
Python :: python alphabet string 
Python :: find common words in two lists python 
Python :: pandas extract month year from date 
Python :: datetime to int python 
Python :: likeliness python 
Python :: get most recent file in directory python 
Python :: python sum attribute in list 
Python :: jupyter notebook check memory usage 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =