β back to unit
Challenge
# queues advanced
The Caterpillar March
A caterpillar of length L inches along the number line: its head advances by each step, and its body is wherever the head recently was. π
The head starts at position 0 (body stacked there too). You get the body length L and the steps (each a positive number). After each step the head lands on a new position; the body occupies the last L head-positions. Print the final body, head first, space-separated.
Example:
Input: L = 3, steps = [2, 3, 1, 4]
Output: 10 6 5
The head walks to 2, then 5, then 6, then 10. Keeping only the last 3 head-positions gives a body of 10 (head), 6, 5.
π‘ need a hint?
q-caterpillar-march.pyπ given lines are locked β write your code in between
loading...