def runningMean(seq, N, M):
    """
     Purpose: Find the mean for the points in a sliding window (fixed size)
              as it is moved from left to right by one point at a time.
      Inputs:
          seq -- list containing items for which a mean (in a sliding window) is
                 to be calculated (N items)
            N -- length of sequence
            M -- number of items in sliding window
      Otputs:
        means -- list of means with size N - M + 1
    """
    # Load deque (d) with first window of seq
    d = deque(seq[0:M])
    means = [sum(d) / len(d)] # contains mean of first window
    # Now slide the window by one point to the right for each new position (each pass through
    # the loop). Stop when the item in the right end of the deque contains the last item in seq
    for item in islice(seq, M, N):
        old = d.popleft()            # pop oldest from left
        d.append(item)               # push newest in from right
        m = sum(d) / len(d)
        means.append(m)     # mean for current window
    return means