Moving Average from Data Stream
https://repl.it/CarT
class MovingAverage(object):
def __init__(self, size):
"""
Initialize your data structure here.
:type size: int
"""
self.window = collections.deque()
self.size = size
self.cur_sum = 0
def next(self, val):
"""
:type val: int
:rtype: float
"""
if len(self.window)==self.size:
left = self.window.popleft()
self.cur_sum-=left
self.window.append(val)
self.cur_sum+=val
return self.cur_sum/float(len(self.window))
# Your MovingAverage object will be instantiated and called as such:
# obj = MovingAverage(size)
# param_1 = obj.next(val)