Simple Text Editor

Sort by

recency

|

258 Discussions

|

  • + 0 comments
    class Stack:
        def __init__(self):
            self.stack = ""
            self.cache = []
            
        def append(self, value):
            self.cache.append(self.stack)
            self.stack += value
        
        def pop_k(self, k_value):
            self.cache.append(self.stack)
            if len(self.stack) < k_value:
                self.stack = ""
            else:
                self.stack = self.stack[:-k_value]
        
        def print_c(self, kth_value):
            if len(self.stack) < kth_value:
                return ""
            return self.stack[kth_value-1]
            
        def undo(self):
            self.stack = self.cache[-1]
            self.cache.pop()
    
  • + 0 comments

    Point 4 is worded confusingly, I'd put it as:

    "4. undo - Undo the last operation of type 1 or 2 , reverting S to the state it was in prior to that operation." and for further clarity it could be added "(it could reach its initial empty state)."

  • + 0 comments
     stack<string>  state;
        int q , t, del, pr;
        int i = 0;
        string s, res;  
        cin>>q;
        while(i < q)
        {   
            cin>>t;   
            if(t ==1)
            {
                //Append 
                cin>>s;
                state.push(res);
                res +=s;
                
            }else if(t ==2)
            {
                //Delete
                cin >> del;
                int index = res.length() - del;
                if(index < res.length())
                {
                    state.push(res);
                    res = res.erase(index,res.length());
                }
                
            }else if(t ==3)
            {
                //Print
                cin>>pr;
                int index = pr -1;
                if(index < res.length())
                {
                    cout<<res[index] << endl;
                }
                
            }else if(t ==4)
            {
                //Undo
                if(!state.empty())
                {           
                    string tp = state.top();
                    res = tp;
                    state.pop();
                }     
            }
            i++;
        }
    
        return 0;
    
  • + 0 comments
    S, history = [], []
    Q = int(input())    # no. of operations
    for i in range(Q):
        op = input().split()
        
        if op[0] == "1":    # append()
            chars = op[1]
            S.extend(chars)
            history.append(("1", chars))
            
        elif op[0] == "2":  # delete()
            k = int(op[1])
            deleted = "".join(S[-k: ])
            history.append(("2", deleted))
            S = S[: -k]
            
        elif op[0] == "3":  # print()
            k = int(op[1]) - 1
            print(S[k])
            
        else:   # undo() either "1" or "2":
            last_op, chars = history.pop()
            if last_op == "1":
                S = S[: -len(chars)]
            elif last_op == "2":
                S.extend(chars)
    
  • + 0 comments

    For anyone else who took too long to debug; while the description demonstrates a list of operations ["1 ,4", "2,5"...] make no mistake that the input being sent to the function you are intended to implement is a string. The first step is parsing the input string which really would be nice if they put that in the description.

    Im starting to have a hard time understanding if this is intentional complexity of Hackerrank or incidental....am I supposed to be looking at this as a poorly implemented code that I need to work around the oddness of and proveI can?