Given a stream of real-time ad interaction events, write a function to calculate the moving average of user engagement within a sliding window.
Events arrive in nondecreasing timestamp order. For each event, include all events whose timestamps fall within the inclusive interval [timestamp - window_size, timestamp], and return the averages in arrival order.
def moving_average(events, window_size): accepts a list of (timestamp, engagement) pairs and a positive integer window size, and returns a list of floating-point averages. Return [] for an empty stream.
def moving_average(events, window_size):