Linear interpolation is a method for filling in missing values in a sequence by drawing a straight line between the nearest known values on each side. It is one of the most common imputation techniques for time series data, preserving the local trend between observed points.
Given a Python list of numbers where some entries are None (missing), fill in the missing values using linear interpolation between the nearest known neighbors. Return a new Python list (do not use NumPy).
For each gap of consecutive None values between known values at positions left and right:
value[j]=vleft+right−leftj−left⋅(vright−vleft)where j is the position of the missing value, and v_left, v_right are the known values bounding the gap.
values: list — A Python list of numbers and None values. The first and last elements are always numbers (never None).list — A new Python list of the same length with all None values replaced by their interpolated values.Input:
values = [1, None, 3]
Output:
[1, 2.0, 3]
The missing value at index 1 is linearly interpolated between 1 (index 0) and 3 (index 2): 1 + (1/2) × (3 - 1) = 2.0.
Input:
values = [0, None, None, 6]
Output:
[0, 2.0, 4.0, 6]
Two missing values between 0 and 6. The gap spans 3 intervals: index 1 gets 0 + (1/3)×6 = 2.0, index 2 gets 0 + (2/3)×6 = 4.0.
Scan through the list. When you find a None, find the index of the last known value (left) and the next known value (right). Then fill each position j in between using the linear formula: left_val + (j - left) / (right - left) * (right_val - left_val).
Copy the input list. Use a while loop to scan for None values. When found, search forward for the next non-None. Compute the span (right - left) and interpolate each missing position proportionally.
Sign in to take notes on this problem
Accepts: array
Linear interpolation is a method for filling in missing values in a sequence by drawing a straight line between the nearest known values on each side. It is one of the most common imputation techniques for time series data, preserving the local trend between observed points.
Given a Python list of numbers where some entries are None (missing), fill in the missing values using linear interpolation between the nearest known neighbors. Return a new Python list (do not use NumPy).
For each gap of consecutive None values between known values at positions left and right:
value[j]=vleft+right−leftj−left⋅(vright−vleft)where j is the position of the missing value, and v_left, v_right are the known values bounding the gap.
values: list — A Python list of numbers and None values. The first and last elements are always numbers (never None).list — A new Python list of the same length with all None values replaced by their interpolated values.Input:
values = [1, None, 3]
Output:
[1, 2.0, 3]
The missing value at index 1 is linearly interpolated between 1 (index 0) and 3 (index 2): 1 + (1/2) × (3 - 1) = 2.0.
Input:
values = [0, None, None, 6]
Output:
[0, 2.0, 4.0, 6]
Two missing values between 0 and 6. The gap spans 3 intervals: index 1 gets 0 + (1/3)×6 = 2.0, index 2 gets 0 + (2/3)×6 = 4.0.
Scan through the list. When you find a None, find the index of the last known value (left) and the next known value (right). Then fill each position j in between using the linear formula: left_val + (j - left) / (right - left) * (right_val - left_val).
Copy the input list. Use a while loop to scan for None values. When found, search forward for the next non-None. Compute the span (right - left) and interpolate each missing position proportionally.
Sign in to take notes on this problem
Accepts: array