Simple Text Editor

Sort by

recency

|

250 Discussions

|

  • + 0 comments

    My answer with Typescript, i create classes to easy resusable

    class SEditor {
        private _stacks: string[];
        private _stacks_cursor: number;
        private get s() { return this._stacks[this._stacks_cursor] }
        
        /**
         * Simple editor with a [stacks] stored string state after every steps,
         * Can undo & redo with [cursor], [redo] will be overriden by [append]&[delete]
         */
        
        constructor(s: string, private ws: WriteStream) {
            this._stacks = [s]
            this._stacks_cursor = 0
        }
    
        public _append(w: string): void {
            this._stacks.splice(this._stacks_cursor + 1)
            this._stacks.push(this.s + w)
            this._stacks_cursor++
        }
        public _delete(k: number): void {
            this._stacks.splice(this._stacks_cursor + 1)
            this._stacks.push(this.s.substring(0, this.s.length - k))
            this._stacks_cursor++
        }
        public _print(k: number): void { this.ws.write(this.s[k - 1] + '\n'); }
        public _undo(): void { this._move_cursor(-1) }
        public _redo(): void { this._move_cursor(+1) }
    
        private _move_cursor(steps: number): void {
            this._stacks_cursor = Math.max(0, Math.min(this._stacks_cursor + steps, this._stacks.length - 1));
        }
    }
    function main() {
        const ws: WriteStream = createWriteStream(process.env['OUTPUT_PATH']);
        const q: number = parseInt(readLine().trim(), 10);
        const s = new SEditor('', ws)
    
        for (let i = 0; i < q; i++) {
            let [t, w] = readLine().split(' ');
            switch (t) {
                case '1': s._append(w); break;
                case '2': s._delete(parseInt(w, 10)); break;
                case '3': s._print(parseInt(w, 10)); break;
                case '4': s._undo(); break;
            }
        }
    }
    
  • + 0 comments

    Java 15 Solution:-

    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) throws IOException {
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            int Q = Integer.parseInt(reader.readLine());
            StringBuilder S = new StringBuilder(); 
            Stack<String> stack = new Stack<>(); 
            while (Q-- > 0) {
                String[] input = reader.readLine().split(" ");
                int type = Integer.parseInt(input[0]);
    
                switch (type) {
                    case 1: 
                        stack.push(S.toString()); 
                        S.append(input[1]);
                        break;
    
                    case 2: 
                        int k = Integer.parseInt(input[1]);
                        stack.push(S.toString()); 
                        S.delete(S.length() - k, S.length());
                        break;
    
                    case 3: 
                        int index = Integer.parseInt(input[1]);
                        System.out.println(S.charAt(index - 1));
                        break;
    
                    case 4: 
                        if (!stack.isEmpty()) {
                            S = new StringBuilder(stack.pop());
                        }
                        break;
                }
            }
        }
    }
    
  • + 0 comments
    #include <bits/stdc++.h>
    #define lli long long int
    using namespace std;
    
    int main() {
    
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        
        stack<char> s;
        stack<string> undo;
        
        lli tc;
        cin >> tc;
        while(tc--) {
            lli x;
            cin >> x;
            if (x == 1) {
                // insert
                string v;
                cin >> v;
    
                string temp = "";
                if (!undo.empty()) {
                    temp = undo.top();
                }
                for (int i = 0; i < v.length(); i++) {
                    temp += v[i];
                    s.push(v[i]);
                }
                undo.push(temp);
            } else if (x == 2) {
                if (s.empty()) continue;
                // delete
                lli y;
                cin >> y;
                while(y--) {
                    s.pop();
                }
                string ab = "";
                stack<char> temp;
                temp = s;
                while(!temp.empty()) {
                    ab += temp.top();
                    temp.pop();
                }
                if (ab.length() > 0) reverse(ab.begin(), ab.end());
                undo.push(ab);
            } else if (x == 3) {
                if (s.empty()) continue;
                // print
                lli y;
                cin >> y;
                string temp = undo.top();
                cout << temp[y-1] << "\n";
            } else {
                // undo
                if (!undo.empty()) {
                    undo.pop();
                    string temp = "";
                    if (!undo.empty()) temp = undo.top();
                    s = stack<char>();
                    for (auto it: temp) {
                        s.push(it);
                    }
                }
            }
        }
        
        return 0;
    }
    
  • + 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);
    }