History beats a single point — next-step prediction
Background
The lesson's motivating example: predicting a ball's next position from its current point alone is underdetermined — you can't tell which way it's moving. Give the model the trajectory and a simple constant-velocity estimate works: the next point is the last point plus the last observed step (velocity).
Problem statement
Implement next_position(trajectory) returning the constant-velocity prediction of the next point.
Input
trajectory— array-like of past points (length ). Each point is a scalar or a coordinate vector (e.g.[x, y]).
Output
Returns an np.ndarray (or scalar array) the shape of one point: .
Examples
Example 1 — 2D straight line
Input: trajectory = [[0, 0], [1, 1], [2, 2]]
Output: [3 3]
Explanation: last step was , so the next point continues to .
Example 2 — 1D
Input: trajectory = [1, 2, 3]
Output: 4
Explanation: velocity → next is .
Constraints
- Use only the last two points:
last + (last - prev). - Return the same shape as a single point.
Notes
- This is the simplest "temporal model" — it uses history (the previous step) rather than just the present. RNNs generalise this by learning what to remember.
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •example 2D line
- •reference 1D velocity
- •sample negative step