Given a list of 3D trajectory points representing a vehicle's path, where each point is [x, y, z], tokenize the trajectory into discrete movement tokens.
Each token represents the direction of movement between consecutive points along each axis: 'F' (forward, a positive change), 'B' (backward, a negative change), or 'S' (stationary, zero change). A token is a string of exactly three characters giving the movement along the x, y and z axes, in that order.
After computing all movement tokens, predict the next step by appending the most frequent token to the end of the token list. If there is a tie in frequency, choose the token that appeared first in the sequence.
Return the list of all tokens, including the predicted next token at the end.
Function Signature
def tokenize_trajectory(points: list[list[int]]) -> list[str]:
Rules
-
The change on an axis is the next point's coordinate minus the current point's coordinate.
-
The result holds one movement token per pair of consecutive points, in order, followed by the predicted token, so its length equals
len(points)
.
-
Among tokens tied for the highest count, the one whose first occurrence has the smallest index wins.
Constraints
-
2 <= len(points) <= 10^4
-
-10^6 <= points[i][j] <= 10^6
-
Each point has exactly 3 coordinates
[x, y, z]
.
Examples
Input: points = [[0, 0, 0], [1, 2, -1], [2, 2, 0], [3, 1, 0]]
Output: ["FFB", "FSF", "FBS", "FFB"]
The three steps are (1, 2, -1), (1, 0, 1) and (1, -1, 0), which give "FFB", "FSF" and "FBS". Each token appears once, and "FFB" appeared first, so it is the prediction.
Input: points = [[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3], [2, 2, 2]]
Output: ["FFF", "FFF", "FFF", "BBB", "FFF"]
The first three steps move forward on every axis ("FFF") and the last moves backward on every axis ("BBB"). "FFF" appears 3 times against once for "BBB", so it is the prediction.
Input: points = [[0, 0, 0], [0, 0, 0], [1, 0, 0], [1, 0, -5], [3, 0, -5], [3, 0, -7]]
Output: ["SSS", "FSS", "SSB", "FSS", "SSB", "FSS"]
The steps are (0, 0, 0), (1, 0, 0), (0, 0, -5), (2, 0, 0) and (0, 0, -2). "FSS" and "SSB" both appear twice. "FSS" first appears at index 1 and "SSB" at index 2, so "FSS" is the prediction, even though the very first token is "SSS".