Tracing a three-sample rolling mean

A pressure logger smooths readings by reporting the mean of each group of three consecutive samples. A rolling sum is a stored total updated by removing the departing sample and adding the arriving sample.


The following Python code processes readings in kPa. Indices start at zero, and range(3, len(readings)) produces 3, 4, and 5.


Python
readings = [18, 24, 21, 27, 30, 24]
window_sum = readings[0] + readings[1] + readings[2]

for i in range(3, len(readings)):
    window_sum = window_sum - readings[i - 3] + readings[i]
    mean = window_sum / 3


Complete the table with the values immediately after each iteration. Enter exact integers; the column headings supply the units.

Loop index iWindow sum (kPa)Mean pressure (kPa)
3
4
5

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Applications of Loops Questions: