Simple Text Editor

Sort by

recency

|

259 Discussions

|

  • + 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()
    
  • + 0 comments

    Point 4 is worded confusingly, I'd put it as:

    "4. undo - Undo the last operation of type 1 or 2 , reverting S to the state it was in prior to that operation." and for further clarity it could be added "(it could reach its initial empty state)."

  • + 0 comments
     stack<string>  state;
        int q , t, del, pr;
        int i = 0;
        string s, res;  
        cin>>q;
        while(i < q)
        {   
            cin>>t;   
            if(t ==1)
            {
                //Append 
                cin>>s;
                state.push(res);
                res +=s;
                
            }else if(t ==2)
            {
                //Delete
                cin >> del;
                int index = res.length() - del;
                if(index < res.length())
                {
                    state.push(res);
                    res = res.erase(index,res.length());
                }
                
            }else if(t ==3)
            {
                //Print
                cin>>pr;
                int index = pr -1;
                if(index < res.length())
                {
                    cout<<res[index] << endl;
                }
                
            }else if(t ==4)
            {
                //Undo
                if(!state.empty())
                {           
                    string tp = state.top();
                    res = tp;
                    state.pop();
                }     
            }
            i++;
        }
    
        return 0;
    
  • + 0 comments
    S, history = [], []
    Q = int(input())    # no. of operations
    for i in range(Q):
        op = input().split()
        
        if op[0] == "1":    # append()
            chars = op[1]
            S.extend(chars)
            history.append(("1", chars))
            
        elif op[0] == "2":  # delete()
            k = int(op[1])
            deleted = "".join(S[-k: ])
            history.append(("2", deleted))
            S = S[: -k]
            
        elif op[0] == "3":  # print()
            k = int(op[1]) - 1
            print(S[k])
            
        else:   # undo() either "1" or "2":
            last_op, chars = history.pop()
            if last_op == "1":
                S = S[: -len(chars)]
            elif last_op == "2":
                S.extend(chars)