Replay a log of stack operations
Updated
A log is a sequence of push/pop (or undo/do) records. Replaying from [] rebuilds the stack. Replaying is how you test, deserialize, or time-travel. Apply in order. Do not skip.
What to focus on
If the log includes pops, they need a stack that actually has those tops. The final stack is the replay result.
Worked examples
rebuild
a = [], log: push 4, push 1, push 9- [4, 1, 9]
Result: [4, 1, 9]
with pops
a = [], log: push 4, push 1, pop, push 9- [4, 9]
Result: [4, 9]
drain then push
a = [], log: push 4, push 1, pop, pop, push 9- [9]
Result: [9]
Try it
Write replay(log). log is a sequence of push and pop records. Replay starts from []. Apply each record in order. Return the final stack. Replay push 4,1,9 → [4, 1, 9]. Replay push 4,1, pop, push 9 → [4, 9]. Replay push 4,1, pop, pop, push 9 → [9].
The log never pops an empty stack. Values are integers. Replay starts from [].