Advertisement
Lyuben_Andreev

TextEditor

Aug 29th, 2024
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | Source Code | 0 0
  1. from collections import deque
  2.  
  3.  
  4. class TextEditor:
  5.     def __init__(self):
  6.         self.text = deque()
  7.         self.undo_stack = deque()
  8.  
  9.     def type_char(self, char):
  10.         self.text.append(char)
  11.         self.undo_stack.clear()
  12.         print(f'Typed: {char}')
  13.  
  14.     def undo(self):
  15.         if self.text:
  16.             last_char = self.text.pop()
  17.             self.undo_stack.append(last_char)
  18.         else:
  19.             print(f'Nothing to undo')
  20.  
  21.     def redo(self):
  22.         if self.undo_stack:
  23.             char = self.undo_stack.pop()
  24.             self.text.append(char)
  25.             print(f'Redo: {char}')
  26.  
  27.         else:
  28.             print('Nothing to redo')
  29.  
  30.     def display_text(self):
  31.         print(f'Current text:', ''.join(self.text))
  32.  
  33.  
  34. editor = TextEditor()
  35. editor.type_char('H')
  36. editor.type_char('e')
  37. editor.type_char('l')
  38. editor.type_char('l')
  39. editor.type_char('o')
  40. editor.type_char('!')
  41. editor.undo()
  42. editor.redo()
  43.  
  44.  
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement