Simple Text Editor

Sort by

recency

|

262 Discussions

|

  • + 0 comments
    s = ''  # Current string state
    history = []  # Stack of undo operations (each is a function that reverts a change)
    
    for _ in range(int(input())):
        command = input().rstrip().split()
        
        match command[0]:
            case '1':  # Append operation
                # Save undo operation (removes the appended part)
                # Using lambda with default arg to capture current command[1] length
                history.append(lambda s, chars_to_remove=len(command[1]): s[:-chars_to_remove])
                # Perform the append
                s += command[1]
                
            case '2':  # Delete operation
                chars_to_delete = int(command[1])
                # Save undo operation (re-adds the deleted portion)
                # Store the deleted substring as default arg to avoid late binding issues
                history.append(lambda s, deleted_part=s[-chars_to_delete:]: s + deleted_part)
                # Perform the deletion
                s = s[:-chars_to_delete]
                
            case '3':  # Print character at index
                print(s[int(command[1]) - 1])
                
            case '4':  # Undo last operation
                if history:  # Only undo if there's history
                    # Apply the most recent undo operation
                    s = history.pop()(s)
    
  • + 0 comments

    Using stack DS

    static StringBuilder res = new StringBuilder(); static StackstringStack = new Stack<>(); public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int q = scanner.nextInt();

        //number of quiries
        while( (q--) > 0 ){
            int typeOfOp= scanner.nextInt();
            switch (typeOfOp){
                case 1:
                    String stringToAppend = scanner.next();
                    append(stringToAppend);
                    break;
                case 2:
                    int k = scanner.nextInt();
                    deleteLast_K_char(k);
                    break;
    
                case 3:
                    int kToPrint = scanner.nextInt();
                    printKchar(kToPrint);
                    break;
    
                case 4:
                    undo(); 
    
            }
    
        }
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    }
    
    
    public static void append(String s){
        stringStack.push(res.toString());
        res.append(s);
    }
    
    public static void deleteLast_K_char(int k){
         stringStack.push(res.toString());
         //System.out.println("before delete ----> " + res);
         res.delete(res.length() - k, res.length());
    
        //System.out.println("after delete ----> "+ res);
    }
    
    public static void printKchar(int k){
        System.out.println(res.charAt(k-1));
    }
    
    public static void undo(){
        res=new StringBuilder(stringStack.pop());
    
    }
    
  • + 0 comments

    My C# solution

    class Solution {
        private enum Operation {
            Append,
            Delete
        }
        
        static void Main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
            
            var numberOfInputs = Int32.Parse(Console.ReadLine()!);
            var theString = string.Empty;
            var operations = new Stack<(Operation, string)>();
            
            for (var _ = 0; _ < numberOfInputs; _++)
            {
                var operation = Console.ReadLine()!.Split();
                
                switch (operation[0])
                {
                    case "1":
                        theString += operation[1];
                        operations.Push(new (Operation.Append, operation[1]));
                        continue;
                    case "2":
                        var k = Int32.Parse(operation[1]);
                        var stringToRemove = theString[(theString.Length - k)..];
                        theString = theString.Substring(0, theString.Length - k);
                        operations.Push(new (Operation.Delete, stringToRemove));
                        continue;
                    case "3":
                        k = Int32.Parse(operation[1]);
                        Console.WriteLine(theString[k - 1]);
                        continue; 
                    case "4":
                        var theOperation = operations.Pop();
                        if (theOperation.Item1 == Operation.Append)
                        {
                            theString = theString.Substring(0, theString.Length - theOperation.Item2.Length);
                            continue;
                        }
                        theString += theOperation.Item2;
                        continue;
                }
            }
        }
    }
    
  • + 0 comments
        static void Main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
            
            int q = int.Parse(Console.ReadLine().Trim());
            
            Editor editor = new Editor();
            
            for(int i = 0; i < q; i++){
                string[] op = Console.ReadLine().Trim().Split(' ');
                
                editor.Execute(int.Parse(op[0]), op.Length > 1 ? op[1] : null);
            }
        }
        
        public class Editor {
            public string Text = string.Empty;
            public Stack<string> Undos = new Stack<string>();
            
            private ITextOperation[] operations = {
                new AppendOperation(),
                new DeleteOperation(),
                new PrintOperation(),
                new UndoOperation()
            };
            
            public void Execute(int opId, string p){
                operations[opId - 1].Execute(this, p);
            }
            
        }
        
        public interface ITextOperation{
            public void Execute(Editor editor, string val);
        }
        
        public class AppendOperation : ITextOperation {
            public void Execute(Editor editor, string val){
                editor.Undos.Push(editor.Text);
                editor.Text += val;
            }
        }
        public class DeleteOperation : ITextOperation {
            public void Execute(Editor editor, string val){
                int k = int.Parse(val);
                editor.Undos.Push(editor.Text);
                editor.Text = editor.Text.Remove(editor.Text.Length - k);
            }
        }
        public class PrintOperation : ITextOperation {
            public void Execute(Editor editor, string val){
                int k = int.Parse(val);
                Console.WriteLine(editor.Text[k-1]);
            }
        }
        public class UndoOperation : ITextOperation {
            public void Execute(Editor editor, string val){
                editor.Text = editor.Undos.Pop();
            }
        }
    
  • + 0 comments
    class Stack:
        def __init__(self):
            self.stack = ""
            self.cache = []
            
        def append(self, value):
            self.cache.append(self.stack)
            self.stack += value
        
        def pop_k(self, k_value):
            self.cache.append(self.stack)
            if len(self.stack) < k_value:
                self.stack = ""
            else:
                self.stack = self.stack[:-k_value]
        
        def print_c(self, kth_value):
            if len(self.stack) < kth_value:
                return ""
            return self.stack[kth_value-1]
            
        def undo(self):
            self.stack = self.cache[-1]
            self.cache.pop()