Trim the zero padding from a sensor log
Your task
A vibration sensor writes zeros to its log while it spins up and again while it winds down. The real capture sits between that padding, and it can contain zeros of its own.
Write trim_zero_padding(readings) that returns the readings without the padding.
Input and output:
readings: a list of up to 100,000 integers, each between -1,000,000 and 1,000,000. It may be empty.- Returns a list of integers.
Rules:
- Remove every zero that comes before the first nonzero reading, and every zero that comes after the last nonzero reading.
- Keep everything from the first nonzero reading to the last nonzero reading, zeros included, in the original order.
- If the log is empty or holds only zeros, return an empty list.
Examples
trim_zero_padding(readings: list[int]) -> list[int]Example 1
Input
trim_zero_padding([0, 0, 4, 0, 5, 0])Output
[4, 0, 5]Example 2
Input
trim_zero_padding([-3, 0, 0, 2])Output
[-3, 0, 0, 2]Example 3
Input
trim_zero_padding([0, 0, 0])Output
[]