A delivery drone receives a stream of velocity commands, each a vector such as [east, north, up] in metres per second. Its motors manage at most limit metres per second in any direction, measured as the straight-line (Euclidean) length of the vector. A command that is too fast must be slowed down to exactly limit, and it must keep pointing the same way: the drone may go slower than asked, never somewhere else. Commands at or under the limit are kept as they are.
Write apply_limit(commands, limit) that returns the list of commands the motors receive, each as a new list.
Examples
Input: commands = [[6, 8], [3, 0], [-9, 12]], limit = 5
Output: [[3.0, 4.0], [3, 0], [-3.0, 4.0]]
Explanation: [6, 8] has length 10, twice the limit, so it is halved. [3, 0] is within the limit.
[-9, 12] has length 15 and is scaled by 1/3.
Capping each component at 5 separately would turn [6, 8] into [5, 5], which points north-east
instead of along the original heading.
Input: commands = [[0, 0, 0], [1, 2, 2]], limit = 0
Output: [[0, 0, 0], [0.0, 0.0, 0.0]]
Constraints
1 <= len(commands) <= 10**4; every command has between 1 and 10 components, ints or floats with absolute value at most10**40 <= limit <= 10**4- answers are compared with a tolerance of
1e-6, so ints and floats are both fine; do not change the input lists
Goals
- Shorten a vector to a given length without changing its direction
- Compute the Euclidean length and a scale factor from it
- See why limiting each component separately changes the direction