Find the Running Median

  • + 0 comments

    Refined Version by importing bisect

    import bisect
    
    def runningMedian(a):
        medians = []
        arr = []
    		
        for i, n in enumerate(a, 1):
            # bisection
            bisect.insort(arr, n)
            
            # median
            m = i // 2
            if i % 2:
                medians.append(round(arr[m], 1))
            else:
                medians.append(round((arr[m] + arr[m-1]) / 2, 1))
    						
        
        return medians