Simple Text Editor

Sort by

recency

|

246 Discussions

|

  • + 1 comment

    I am at a loss on this one. Many of the test cases are failing, yet my output when I run locally matches the expected output perfectly. I get 'wrong answer'. If anyone has a clue what it could be let me know. My code:

    private static final Scanner scanner = new Scanner(System.in);
    private static Stack<String> state = new Stack<>();
    
    public static void main(String[] args) throws IOException {
        state.push("");
        while (scanner.hasNext()) {
            String command = scanner.nextLine();
            int op = command.charAt(0) - '0';
            switch (op) {
                case 1:
                    String toAppend = command.substring(2);
                    state.push(append(state.peek(), toAppend));
                    break;
                case 2:
                    int charsToDelete = Integer.parseInt(command.substring(2));
                    state.push(delete(state.peek(), charsToDelete));
                    break;
                case 3:
                    int charToPrint = Integer.parseInt(command.substring(2));
                    System.out.println(state.peek().charAt(charToPrint - 1));
                    break;
                case 4:
                    state.pop();
                    break;
            }
    
        }
    }
    
    private static String append(String text, String toAppend) {
        return text + toAppend;
    }
    
    private static String delete(String text, int charsToDelete) {
        return text.substring(0, text.length() - charsToDelete);
    }
    
  • + 0 comments

    straightforward solution in python:

    intial_input = input().split(" ")
    n = int(intial_input[0])
    if len(intial_input) > 1:
        s = str(intial_input[1])
    else:
        s = ""
    undo = []
    for i in range(n):
        operation = input().split(" ")
        if operation[0] == '1':
            undo.append(s)
            s = s + (operation[1])
        elif operation[0] == '2':
            undo.append(s)
            x = len(s)
            s = s[0:x-int(operation[1])]
        elif operation[0] == '3':
            x = int(operation[1])-1
            print(s[x])
        elif operation[0] == '4':
            s = undo.pop()
      
    
  • + 0 comments

    This doesn't look like an intermediate level problem. Seems much easier that 'Lego Blocks' for example.

  • + 0 comments

    include

    include

    include

    include

    include

    include

    using namespace std;

    class S { private: string my_string{};

    public: 
    
    void append (string str){
        my_string.append(str);
    }
    
    void deletechar (int k){
        my_string.erase(my_string.size() - k);
    }
    
    void print(int k){
        cout << my_string[k-1] <<endl;
    }
    
    //constructor
    S (string str)
    :my_string{str} {}
    
    S(){}
    
    S &operator= ( const S &lhs){
        this->my_string = lhs.my_string;
        return *this;
    }
    

    };

    int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int Q; cin >> Q;

        int op;
        S my_string;
    
        stack<S> stk;
    
       while(Q--){
         cin >> op;
         string str;
         int k;
         switch(op) {
             case 1: 
             cin >> str;
             my_string.append(str);
             stk.push(my_string);
             break;
    
             case 2:
             cin >> k;
             my_string.deletechar(k);
             stk.push(my_string);
             break;
    
             case 3:
             cin >> k;
             my_string.print(k);
             break;
    
             case 4:
             if (!stk.empty()) {
                    stk.pop();
                    if (!stk.empty()) {
                        my_string = stk.top();
                    } else {
                        my_string = S(); // Reset to default state if the stack is empty
                    }
                }
                break;
             break;
    
             default:
             return 0;
         }  
    
       }
    return 0;
    

    }

  • + 0 comments

    PYTHON SOLUTION

    APPEND = 1 DELETE = 2 PRINT = 3 UNDO = 4

    class TextEditor: def init(self, initial_str: str = ""): self.current_str = list(initial_str) self.stack = []

    def append(self, s: str):
    
            self.stack.append("".join(self.current_str))
    
            self.current_str.extend(s)
    
    def delete(self, k: int):
    
            self.stack.append("".join(self.current_str))
    
            self.current_str = self.current_str[:-k]
    
    def print(self, k: int):
    
            print(self.current_str[k-1])
    
    def undo(self):
    
            if self.stack:
                    self.current_str = list(self.stack.pop())
    
    def command(self, s: str):
    
            parts = s.split(maxsplit=1)
            op = int(parts[0])
    
            if op == APPEND:
                    self.append(parts[1])
            elif op == DELETE:
                    self.delete(int(parts[1]))
            elif op == PRINT:
    

    self.print(int(parts[1])) elif op == UNDO: self.undo()

    if name == "main": Q = int(input()) editor = TextEditor() for _ in range(Q): command = input().strip() editor.command(command)