easy +8 pts

Linear Interpolation Array

Fill missing values in a list using linear interpolation between known points.

Write a function `interpolate_array(values)` that takes a list of numbers and `None` placeholders and returns a new list where every `None` is replaced by the linear interpolation between the nearest known non-None values on both sides. The interpolation must be **smoothly distributed** across the missing gap. Specifically, if the gap starts at index `start` and ends at index `end` (inclusive) where `values[start]` and `values[end]` are known and all indices between are `None`, then for each position `i` in `(start, end)` the interpolated value is: ``` values[start] + (values[end] - values[start]) * (i - start) / (end - start) ``` Rules: - If the list has no `None`, return a copy (or the same values) unchanged. - If the first element is `None`, treat the first known value as the left anchor (i.e., fill leading `None`s with the first known value). - If the last element is `None`, treat the last known value as the right anchor (i.e., fill trailing `None`s with the last known value). - It is guaranteed that the list contains at least one non-`None` value. - All results should be returned as floats (or ints if division yields an integer). **Input:** A list `values` of length between 1 and 1000, containing integers, floats, or `None`. **Return:** A list of numbers (int or float) with the same length, with `None` replaced as described.

Constraints

Length of input list: 1 to 1000. At least one element is not None. Values can be any real numbers. Time complexity: O(n).

Example

>>> interpolate_array([1, None, None, 4])
[1, 2.0, 3.0, 4]
>>> interpolate_array([None, 10, None, 20])
[10, 10, 15.0, 20]
>>> interpolate_array([1, 2, 3])
[1, 2, 3]
8 points ~12 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Find indices of known values. If a gap is between two known indices, apply the formula.
Handle leading None by copying the first known value; similarly for trailing None.
You can create a copy of the list and mutate it, or build a new list from scratch.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.