Sliding window sums

Write codeEasy12 min

Your task

Given a list of integers and a window width w (w >= 1), return the sums of every contiguous window of exactly w elements, left to right. The result has length n - w + 1 for a list of length n; if w exceeds n, return an empty list. Values may be negative. Example: values = [1, 3, 2, 6], w = 2 gives [4, 5, 8].

Examples

window_totals(values: list[int], w: int) -> list[int]
Example 1
Inputwindow_totals([1, 3, 2, 6], 2)
Output[4, 5, 8]