Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Ex 2:

To use undo operation in a Microsoft Word, the machine needs to remember the list of
states and operations made. To implement this in real time write a detailed program
with set of insertion and deletion operation functions.

Algorithm:
1. Initialize TextEditor:
• Initialize a TextEditor object with an empty initial text and an empty history
list.
2. Insert Text Operation:
• Define an insert_text method in the TextEditor class.
• Accept the text to be inserted (inserted_text) as a parameter.
• Create a tuple representing the operation (("insert", current_text,
inserted_text)).
• Append the tuple to the history list.
• Update the current text by concatenating it with the inserted text.
3. Delete Text Operation:
• Define a delete_text method in the TextEditor class.
• Accept the number of characters to delete (num_chars) as a parameter.
• Extract the deleted text from the end of the current text.
• Create a tuple representing the operation (("delete", current_text,
deleted_text)).
• Append the tuple to the history list.
• Update the current text by removing the last num_chars characters.
4. Undo Operation:
• Define an undo method in the TextEditor class.
• Check if the history list is not empty.
• Pop the last tuple from the history list.
• Extract the operation type, the current text, and the affected text.
• Based on the operation type:
• If it was an "insert" operation, update the current text to the previous
state.
• If it was a "delete" operation, update the current text to the previous
state by adding the affected text back.
5. Display Text:
• Define a display_text method in the TextEditor class.
• Print the current text.
6. Example Usage:
• Create a TextEditor object.
• Use the insert_text and delete_text methods to perform operations.
• Use the undo method to revert the last operation.
• Use the display_text method to show the current text.

Program:
class TextEditor:
def __init__(self):
self.text = ""
self.history = []

def insert_text(self, inserted_text):


# Insert operation
new_text = self.text + inserted_text
# Save state to history
self.history.append(("insert", self.text, inserted_text))
# Update current text
self.text = new_text

def delete_text(self, num_chars):


# Delete operation
deleted_text = self.text[-num_chars:]
new_text = self.text[:-num_chars]
# Save state to history
self.history.append(("delete", self.text, deleted_text))
# Update current text
self.text = new_text

def undo(self):
if not self.history:
print("Nothing to undo.")
return

# Get the last state from history


operation, current_text, affected_text = self.history.pop()

# Perform undo based on the operation


if operation == "insert":
self.text = current_text
elif operation == "delete":
self.text = current_text

def display_text(self):
print("Current Text:", self.text)

# Example Usage:
editor = TextEditor()

editor.insert_text("Welcome to ")
editor.display_text()

editor.insert_text("python programming")
editor.display_text()

editor.delete_text(11)
editor.display_text()

editor.undo()
editor.display_text()
Output:
Current Text: Welcome to

Current Text: Welcome to python programming

Current Text: Welcome to python

Current Text: Welcome to python programming

You might also like