This question evaluates understanding of piecewise linear interpolation, numerical reasoning, and the ability to handle ordered 2D point data and edge cases such as exact-coordinate matches.
Given a list of 2D points [(x1, y1), (x2, y2), ..., (xn, yn)], where the points are ordered by strictly increasing x, connect each consecutive pair of points with a straight line segment to form a piecewise linear function.
Write a function that returns the function value at a target x.
Assumptions:
target_x
is guaranteed to lie within the range
[x1, xn]
.
target_x
is exactly equal to one of the input
x
coordinates, return the corresponding
y
value.
target_x
and use linear interpolation to compute the result.
Your function should take:
points
: a list of
(x, y)
pairs
target_x
: a numeric value
and return:
y
value at
target_x