mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 12:16:14 -04:00
26 lines
517 B
Python
26 lines
517 B
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
# author: bt3gl
|
|
|
|
'''
|
|
Given a stream of integers and a window size, calculate the moving average in the sliding window.
|
|
'''
|
|
|
|
class MovingAverage:
|
|
|
|
def __init__(self, size: int):
|
|
|
|
self.queue = []
|
|
self.size = size
|
|
|
|
|
|
def next(self, val: int) -> float:
|
|
|
|
self.queue.append(val)
|
|
|
|
if len(self.queue) > self.size:
|
|
self.queue.pop(0)
|
|
|
|
return sum(self.queue) / len(self.queue)
|
|
|