All problems
Command Pattern Apply/Undo
Problem
Simulate a counter driven by reversible command objects.
operations contains "apply" (add values[i] to the counter and push the command), "undo" (revert the most recent applied command), "redo" (reapply the most recently undone command), or "value" (record the counter). A new apply clears the redo stack.
Return the list of recorded results (None for apply, undo, and redo).
Hint
Commands are deltas, so undo subtracts and redo adds the stored amount.
Test cases
case 1
in: [["apply","apply","undo","value","redo","value"],[3,4,0,0,0,0]]
expected: [null,null,null,3,null,7]
case 2
in: [["undo","value"],[0,0]]
expected: [null,0]
case 3
in: [["redo"],[0]]
expected: [null]
case 4
in: [["apply","undo","apply","redo","value"],[5,0,10,0,0]]
expected: [null,null,null,null,10]
Starter codePython
def command_ops(operations, values):
# Your code here
pass4 test cases · run your code against them in the editor