Simple Text Editor

Sort by

recency

|

256 Discussions

|

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

  • + 0 comments

    C# Solution using Linked List

    class Solution {
        static void Main(String[] args) {
            
            var linkedList = new LinkedList<string>();
            var queries = int.Parse(Console.ReadLine());
            
            for (var i = 0; i < queries; i++) {
                var ops = Console.ReadLine().Split();
                
                switch(ops[0]) {
                    case "1":
                        Append(linkedList, ops[1]);
                        break;
                    case "2":
                        DeleteCharacters(linkedList, int.Parse(ops[1]));
                        break;
                    case "3":
                        Print(linkedList, int.Parse(ops[1])-1);
                        break;
                    case "4":
                        UndoOperation(linkedList);
                        break;   
                }
            }
            
            static void Append(LinkedList<string> l, string data) {
                var s = data;
                if (l.Last != null) {
                    s = $"{l.Last.Value}{data}";
                }
                
                l.AddLast(s);
            }
            
            static void DeleteCharacters(LinkedList<string> l, int k) {
                if (l.Last == null) {
                    return;
                }
                
                var s = l.Last.Value;
                var index = s.Length - k;
                s = s.Remove(index);
                l.AddLast(s);
            }
            
            static void Print(LinkedList<string> l, int index) {
                if (l.Last == null) {
                    return;
                }
                
                var s = l.Last.Value[index];
                Console.WriteLine(s);
            }
            
            static void UndoOperation(LinkedList<string> l)
            {
                if (l.Last == null) {
                    return;
                }
                
                l.RemoveLast();
            }
        }
    }
    
  • + 1 comment

    Anyone have any idea why this is not working

    function processData(input) {
        //Enter your code here
        let numberOfOpps = parseInt(input[0]);
        let inputs = input.split("\n").slice(1); 
        let stack = []; 
        let currentString = ""; 
        for(let i = 0; i < numberOfOpps; i++){ 
             let opperation = inputs[i].split(" "); 
             let code = parseInt(opperation[0]); 
             let string = opperation[1];
             if(code == 1){ 
                stack.push(currentString); 
                currentString = currentString.concat(string); 
                // console.log("CURRENT STRING", currentString); 
             }else if (code == 2){ 
                stack.push(currentString); 
                let index = (currentString.length - parseInt(string) -1);
                if(index <= 0){ 
                    currentString = ""; 
                }else{
                    currentString = currentString.slice(0, index);
                }
                // console.log("CURRENT STRING", currentString);
             }else if (code == 3){ 
                let index = (parseInt(string) - 1);
                 console.log(currentString[index]);
             }else if(code == 4){ 
                currentString = stack.pop(); 
                // console.log("CURRENT STRING", currentString);
             }
        }
    }