Skip to contentKEEP THE CRAFTTHE SANCTUARY

Slide a Fixed Window

Slide a window of fixed width: add the value coming in, drop the value going out, record the sum.

The problem

Implement fixed_window_sums(values, width). Return a list with the sum of every block of width consecutive values, from left to right. For example, [2, 1, 5, 1, 3] with width 3 gives [8, 7, 9]. width is at least 1; if it is larger than len(values), there is no full window, so return an empty list.

Starter code

def fixed_window_sums(values, width):
    pass

Public examples

  1. values = [2, 1, 5, 1, 3], width = 3gives[8, 7, 9]
  2. values = [4, -2, 7], width = 1gives[4, -2, 7]

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.