Simple Text Editor

Sort by

recency

|

29 Discussions

|

  • + 0 comments

    Java

    try(Scanner scan = new Scanner(System.in)){
    
    	int n = scan.nextInt();
    	scan.nextLine();
    
    	Stack<String> history = new Stack<>();
    	String text="" ;
    
    	for (int i = 0; i < n; i++) {
    		int command = scan.nextInt();
    		switch(command){
    			case 1:
    			history.push(text);
    			text+=scan.nextLine().trim();
    			break;
    			case 2:
    			history.push(text);
    			text = text.substring(0, text.length()-scan.nextInt());
    			break;
    			case 3:
    			System.out.println(text.charAt(scan.nextInt()-1));
    			break;
    			case 4:
    			text= history.pop(); 
    			break;                    
    		}
    	}
    }        
    
  • + 0 comments

    My ansewr in python

    Q = int(input())
    S = str()
    prev = []
    
    operations = []
    for i in range(Q):
        operations.append(input().split(" "))
        
    for i, op in enumerate(operations):
        match op[0]:
            case "1":
                prev.append(S)
                S += op[1]
            case "2":
                prev.append(S)
                S = S[:-int(op[1])]
            case "3":
                print(S[int(op[1])-1])
            case "4":
                S = prev[-1]
                prev.pop()
    
  • + 0 comments

    Java O(1)

    public static void main(String[] args) throws IOException {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));
    
            int Q = Integer.parseInt(bufferedReader.readLine().trim());
            StringBuilder S = new StringBuilder();
            Stack<String> history = new Stack<>();
    
            for (int i = 0; i < Q; i++) {
                String[] inputs = bufferedReader.readLine().split(" ");
                int type = Integer.parseInt(inputs[0]);
    
                switch (type) {
                    case 1: // Append(W)
                        history.push(S.toString());
                        S.append(inputs[1]);
                        break;
                    case 2: // Delete(k)
                        int k = Integer.parseInt(inputs[1]);
                        history.push(S.toString());
                        S.delete(S.length() - k, S.length());
                        break;
                    case 3: // Print(k)
                        int index = Integer.parseInt(inputs[1]) - 1;
                        bufferedWriter.write(S.charAt(index));
                        bufferedWriter.newLine();
                        break;
                    case 4: // Undo
                        S = new StringBuilder(history.pop());
                        break;
                }
            }
    
            bufferedReader.close();
            bufferedWriter.flush();
            bufferedWriter.close();
        }
    
  • + 0 comments

    JS

    function processData(input) {
        const undo = [""];
        const operations = [
            (append) => undo.push(undo.at(-1) + append),
            (_delete) => undo.push(undo.at(-1).slice(0, undo.at(-1).length - _delete)),
            (print) => console.log(undo.at(-1).at(print - 1)),
            () => undo.pop()
        ];
        input.split("\n").slice(1).forEach((operation) => operations[operation.split(" ")[0] - 1](operation.split(" ")[1]));
    }
    
  • + 0 comments

    java 8

    public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            Scanner input = new Scanner(System.in);
            int q = input.nextInt();
            
            Stack<String> stack = new Stack<>();
            StringBuilder sb = new StringBuilder();
            
            for(int j = 0; j < q; j++) {
                int choose = input.nextInt();
                switch(choose) {
                    case 1:
                        stack.push(sb.toString());
                        String newText = input.next();
                        sb.append(newText);
                        break;
                    case 2:
                        stack.push(sb.toString());
                        int k = input.nextInt();
                        sb.delete(sb.length() - k, sb.length());
                        break;
                    case 3:
                        int kth = input.nextInt();
                        System.out.println(sb.charAt(kth - 1));
                        break;
                    case 4:
                        sb.setLength(0);
                        sb.append(stack.pop());
                        break;
                }
            }
        }