Dashboard Temp Share Shortlinks Frames API

HTMLify

implement-undo-redo.py
Views: 2 | Author: prakhardoneria
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution:
    def __init__(self):
        self.history = []
        self.future = []

    def append(self, x):
        self.history.append(x)
        self.future.clear()

    def undo(self):
        if self.history:
            self.future.append(self.history.pop())

    def redo(self):
        if self.future:
            self.history.append(self.future.pop())

    def read(self):
        return "".join(self.history)