Sort by

recency

|

551 Discussions

|

  • + 0 comments
    n_opperations = int(input().strip())
    operations = list()
    
    for _ in range(n_opperations):
        operations.append(input().strip())
    
    string = str()
    done = list()
    
    for ops in operations:
        if ops[0] == '1':
            done.append(ops)
            string += ops[2:]
        elif ops[0] == '2':
            k = int(ops[2:])
            done.append((ops[0], ' ', string[-k:]))
            string = string[:-k]
        elif ops[0] == '3':
            print(string[int(ops[2:])-1])
        elif ops[0] == '4':
            if done[-1][0] == '1':
                string = string[:-len(done[-1][2:])]
                done.pop()
            else:
                string += done[-1][2]
                done.pop()
    
  • + 0 comments

    python 3 (OOP)

    class text_editor:
        def __init__(self):
            self.S = ""
            self.op_stack = []
            self.tgt_stack = []
               
        def w_append(self,tgt):
            s1 = list(self.S)
            s1.append(tgt)
            self.S = ''.join(s1)
            self.op_stack.append('1')
            self.tgt_stack.append(len(tgt))
            
        def k_delete(self,tgt):
            s1 = list(self.S)
            self.tgt_stack.append(''.join(s1[-int(tgt):]))
            s1 = s1[:-int(tgt)]
            self.S = ''.join(s1)
            self.op_stack.append('2')
            
        def k_print(self,tgt):
            s1 = list(self.S)
            print(f'{s1[int(tgt) -1]}')
    
        def undo(self):
            s1 = list(self.S)
            op = self.op_stack.pop(-1)
            if op == '1':
                s1 = s1[:-int(self.tgt_stack.pop(-1))]
            elif op == '2':
                s1.append(self.tgt_stack.pop(-1))
            if s1:
                self.S = ''.join(s1)
            else:
                self.S = ""
            
        def receive_ops(self,op,tgt):
            if op == '1':
                self.w_append(tgt)
    
            elif op == '2':
                self.k_delete(tgt)
    
            elif op == '3':
                self.k_print(tgt)
            elif op == '4':
                self.undo()
                       
    if __name__ == '__main__':
        q = int(input())
        S = ""
        tgt = None
        op = ''
        t = text_editor()
        for _ in range(q):
            x = input().split()
            op = x[0]
            if len(x) > 1:
                tgt = x[1]
            t.receive_ops(op,tgt)
    
  • + 0 comments
    my c++ sol:
    
    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <stack>
    using namespace std;
    
    
    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */
        string s="";
        stack<string> st;
        int n;
        cin>>n;
        for(int i =0; i< n; i++)
        {
            int k =0;
            cin>>k;
            if(k==1)
            {
                string str;
                cin>>str;
                st.push(s);
                s.append(str);
            }
            if(k==2)//last k character
            {
                int no;
                cin>>no;
                st.push(s);
                while(no>0)
                {
                    s.pop_back();
                    no--;
                }
            }
            if(k==3)//print kth char
            {
                int h;
                cin>>h;
                cout<<s[h-1]<<endl;
            }
            else if (k == 4) {
                if (!st.empty()) {
                    s = st.top();
                    st.pop();
                }
            }
        }
        return 0;
    }
    
  • + 0 comments

    My C++ Solution:

    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
        int q=0;
        cin >> q;
        string s ="";
        stack<string> v;
        v.push(s);
        for(int i=0; i<q; i++){
            int op = 0;string ss;
            cin >> op;
            if(op != 4)cin >> ss;
            switch (op) {
                case 1:
                   s+=ss;
                   v.push(s);
                   break;
                case 2:
                    s = s.substr(0, s.length() - stoi(ss));
                    v.push(s);
                    break;
                 case 3:
                    cout << v.top()[stoi(ss)-1]<<endl;
                    break;
                case 4:
                    v.pop();
                    s = v.top();
                    break;
            }
        }
        
        return 0;
    }
    
  • + 0 comments
    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
    public class Solution {
    
        public static void main(String[] args) {
          
            Scanner sc = new Scanner(System.in);
            String str = "";
            int top = 0;
            int q = Integer.parseInt(sc.nextLine());
            MyStack stack = new MyStack(q);
            for(int i = 0; i < q; ++i){
                String st[] = sc.nextLine().split(" ");
                int query = Integer.parseInt(st[0]);
                if(query == 1){
                    Node newNode = new Node(query,str.length());
                    stack.top++;
                    stack.list[stack.top] = newNode;
                    str += st[1];
                } else if(query == 2){
                    int k = Integer.parseInt(st[1]);
                    Node newNode = new Node(query,str.substring(str.length()-k));
                    stack.top++;
                    stack.list[stack.top] = newNode;
                    str = str.substring(0,str.length()-k);
                } else if(query == 3){
                    int index = Integer.parseInt(st[1]);
                    System.out.println(str.charAt(index-1));
                } else if(query == 4){
                    Node newNode = stack.list[stack.top];
                    stack.top--;
                    if(newNode.qtype == 1){
                        str = str.substring(0,newNode.idx);
                    } else if(newNode.qtype == 2){
                        str += newNode.w;
                    }
                }
            }
        }
    }
    class MyStack{
        Node list[];
        int top;
        MyStack(int size){
            this.list = new Node[size];
            this.top = -1;
        }
    }
    class Node{
        int qtype;
        int idx;
        String w;
        Node(int x, String y){
            this.qtype = x;
            this.w = y;
        }
        Node(int x, int index){
            this.qtype = x;
            this.idx = index;
        }
    }