Simple Text Editor

Sort by

recency

|

247 Discussions

|

  • + 0 comments

    My python solution

    if name == 'main': text = [] edition = [] for _ in range(int(input())): op = input().split() if op[0] == '1': text.extend(op[1]) edition.append(len(op[1])) elif op[0] == '2': edition.append([]) for _ in range(int(op[1])): edition[-1].append(text.pop()) elif op[0] == '3': print(text[int(op[1])-1]) elif op[0] == '4': undo = edition.pop() if isinstance(undo, int): for _ in range(undo): text.pop() else: text.extend(undo[::-1])

  • + 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;
    

    }