Given a stream of noisy pressure sensor data, implement a smoothing system that provides clean values for mission control.
You are given an array of integer pressure readings and a window size k. For each reading, compute the moving average of the last k readings (including the current one). If fewer than k readings have been received so far, compute the average of all readings received so far.
Return an array of smoothed values, where each value is the floor of the moving average at that point in the stream.
Function Signature
def smooth_pressure_data(readings: list[int], k: int) -> list[int]:
Rules
-
The value at index
i
is computed over
readings[max(0, i - k + 1) . i]
, both ends included.
-
Floor means rounding toward negative infinity, so an average of -3.5 becomes -4 and an average of 0.5 becomes 0.
-
The output has the same length as
readings
.
Constraints
-
1 <= len(readings) <= 10^5
-
-10^4 <= readings[i] <= 10^4
-
1 <= k <= len(readings)
Examples
Input: readings = [1020, 1035, 1015, 1040, 1025, 1030], k = 3
Output: [1020, 1027, 1023, 1030, 1026, 1031]
The windows are [1020], [1020, 1035], [1020, 1035, 1015], [1035, 1015, 1040], [1015, 1040, 1025] and [1040, 1025, 1030], with sums 1020, 2055, 3070, 3090, 3080 and 3095. Dividing each sum by its window size and taking the floor gives the output.
Input: readings = [10, 20, 30, 40, 50], k = 1
Output: [10, 20, 30, 40, 50]
With k = 1 the moving average is just the current reading, so the output matches the input exactly.
Input: readings = [-3, -4, 5], k = 2
Output: [-3, -4, 0]
The second window [-3, -4] averages -3.5, whose floor is -4. The third window [-4, 5] averages 0.5, whose floor is 0.