Instagram
youtube
Facebook
Twitter

Simple Text Editor| Stack

TASK(medium)

Implement a simple text editor. The editor initially contains an empty string. Perform operations of the following types:

  1. append - Append string to the end of.
  2. delete - Delete the last characters of.
  3. print - Print the character of.
  4. undo - Undo the last (not previously undone) operation of type or, reverting to the state it was in prior to that operation.

Example

operation

index   S       ops[index] explanation

-----   ------  ----------  -----------

0       abcde   1 fg        append fg

1       abcdefg 3 6         print the 6th letter - f

2       abcdefg 2 5         delete the last 5 letters

3       ab      4           undo the last operation, index 2

4       abcdefg 3 7         print the 7th characgter - g

5       abcdefg 4           undo the last operation, index 0

6       abcde   3 4         print the 4th character - d

The results should be printed as:

f

g

d

Input Format

The first line contains an integer, Q, denoting the number of operations.
Each line i of the Q subsequent lines (where 0<i<Q) defines an operation to be performed. Each operation starts with a single integer, t(where t € {1,2,3,4}), denoting a type of operation as defined in the Problem Statement above. If the operation requires an argument, is followed by its space-separated argument. For example, if and, line will be 1 abcd.

Constraints

1<=Q<=106
1<=K<=|S|
The sum of the lengths of all W in the input 106.
The sum of k over all delete operations <=2106.
All input characters are lowercase English letters.
It is guaranteed that the sequence of operations given as input is possible to perform.

Output Format

Each operation of type 3 must print the kth character on a new line.

Sample Input

STDIN   Function

-----        --------

8             Q = 8

1 abc      ops[0] = '1 abc'

3 3           ops[1] = '3 3'

2 3            ...

1               xy

3 2

4

4

3 1

Sample Output

c

y

a

SOLUTION 1

n = int(input())

data = str()

log = list()

for i in range(n):

    operator = input() 

    if len(operator.split()) == 2:

        op, w = operator.split()

        if op == '1':

            log.append(data)

            data += w

        elif op == '2':

            log.append(data)

            data = data[:-int(w)]

        elif op == '3':

            print(data[int(w) - 1])   

    else:

        data = log.pop()

 

Explanation Steps

1.  Initialization: text variable is initialized as an empty string to store the text content.

2. Menu: A simple menu is displayed using print statements, allowing the user to choose between inserting text (1), deleting text (2), displaying text (3), or exiting (4).

3. Operations:

  • Insert Text: Appends the user-entered text (insert_text) to the text variable.
  • Delete Text: Uses string slicing (text[:start_index] + text[start_index + length:]) to delete a specified portion of text based on user input (start_index and length).
  • Display Text: Prints the current content of the text variable.
  • Exit: Breaks out of the while loop and exits the program.

4. Error Handling: Basic input validation is implemented to ensure the user enters valid choices and inputs for delete operations.