Belt commands
MediumOnline Assessment
Simulate a conveyor belt that processes a list of commands.
The belt initially holds items, front first. Apply commands in order; each is [operation, value]:
"push": appendvalueto the end of the belt."pop": remove the first item, if there is one.valueis ignored."rotate": move the firstvalue % nitems to the end, in order, wherenis the current belt length (so[1, 2, 3, 4]rotated by 1 becomes[2, 3, 4, 1]). If the belt is empty, do nothing.
Input: items, a list of integers, and commands, a list of [operation, value] pairs.
Output: the belt after all commands, front first.
Example 1
- Input:
- [[1,2,3],[["rotate",1],["push",4],["pop",0]]]
- Output:
- [3,1,4]
- Explanation
- [1, 2, 3] rotated by 1 is [2, 3, 1]. Pushing 4 gives [2, 3, 1, 4]. Popping removes the front item 2, leaving [3, 1, 4].
Example 2
- Input:
- [[5,6,7,8,9],[["rotate",7],["pop",0],["push",1]]]
- Output:
- [8,9,5,6,1]
- Explanation
- The belt has 5 items, so rotating by 7 is the same as rotating by 7 % 5 = 2: [7, 8, 9, 5, 6]. Pop removes 7, then push adds 1 at the end: [8, 9, 5, 6, 1].
Example 3
- Input:
- [[],[["pop",0],["rotate",3],["push",5],["rotate",1]]]
- Output:
- [5]
- Explanation
- On an empty belt pop and rotate do nothing. Push 5 gives [5]. Rotating a one-item belt by 1 (1 % 1 = 0) changes nothing, so the result is [5].
Constraints
0 <= items.length <= 2,000and0 <= commands.length <= 2,000operationis one of"push","pop","rotate"0 <= value <= 1,000,000, and every item is a non-negative integer