You are building a layers panel for a Figma-like design tool. The panel maintains an ordered list of layers, where index 0 represents the top-most layer (rendered last, visible on top) and the last index represents the bottom-most layer.
Given an array of operations describing user actions, simulate the layers panel and return the final list of layer names ordered from top to bottom.
The supported operations are:
-
"addLayer <name>"
: insert a new layer named
<name>
at the top of the stack.
-
"deleteLayer <name>"
: remove the layer named
<name>
if it exists; otherwise do nothing.
-
"moveUp <name>"
: move the layer named
<name>
one position closer to the top.
-
"moveDown <name>"
: move the layer named
<name>
one position closer to the bottom.
-
"undo"
: revert the most recent state-changing operation (
addLayer
,
deleteLayer
,
moveUp
,
moveDown
).
-
"redo"
: re-apply the most recently undone operation.
Any new state-changing operation clears the redo history. undo and redo themselves are not pushed onto the undo stack.
Function Signature
def layers_panel(operations: list[str]) -> list[str]:
Rules
-
The panel starts empty. The returned list is ordered from top (index
0
) to bottom.
-
moveUp
moves a layer to the next smaller index and
moveDown
to the next larger index, swapping it with the neighbor in that direction.
-
An operation that would change nothing is ignored completely:
deleteLayer
,
moveUp
or
moveDown
on a name that is not in the panel,
moveUp
on the top layer, and
moveDown
on the bottom layer. An ignored operation is not added to the undo history, so a later
undo
skips past it, and it does not clear the redo history.
-
undo
with nothing to undo and
redo
with nothing to redo do nothing.
-
undo
restores the panel exactly as it was before the reverted operation, including the position of a deleted layer.
Constraints
-
1 <= operations.length <= 10^4
-
Each operation is one of:
addLayer name
,
deleteLayer name
,
moveUp name
,
moveDown name
,
undo
,
redo
, with a single space between the command and the name.
-
addLayer name
adds a new layer with a unique
name
to the top of the stack: the name is never one that is currently in the panel.
-
1 <= name.length <= 20
, names are lowercase letters and digits.
Examples
Input: operations = ["addLayer rect", "addLayer circle", "addLayer text", "moveDown circle"]
Output: ["text", "rect", "circle"]
The three adds build [text, circle, rect] from top to bottom. moveDown circle then swaps circle with rect, the layer just below it.
Input: operations = ["addLayer a", "addLayer b", "addLayer c", "undo", "undo", "redo"]
Output: ["b", "a"]
The adds build [c, b, a]. The two undos remove c and then b, leaving [a], and the redo puts b back on top to give [b, a].
Input: operations = ["addLayer a", "addLayer b", "moveUp b", "undo", "moveUp a", "redo", "undo", "addLayer c", "redo"]
Output: ["c", "a"]
Both moveUp operations target the top layer, so they are ignored: the first undo removes b, and the redo after moveUp a still restores [b, a]. The second undo removes b again, then addLayer c gives [c, a] and clears the redo history, so the final redo does nothing.