Simple Text Editor

Sort by

recency

|

60 Discussions

|

  • + 0 comments

    On the one hand

    The editor initially contains an empty string, .

    On the other hand, Why does your example start with inital value of S?!

    wtf

  • + 0 comments

    JavaScript Solution:-

    function processData(input) {
        let currentString = '';
        let operationHistory = [];
        const operations = input.split('\n').slice(1); 
        for (let op of operations) {
            const opArr = op.split(' ');
            const type = parseInt(opArr[0]);
            if (type === 1) { 
                const strToAppend = opArr[1];
                currentString += strToAppend;
                operationHistory.push({ type: 1, value: strToAppend }); 
            } else if (type === 2) { // Delete operation
                const numToDelete = parseInt(opArr[1]);
                const deletedString = currentString.slice(-numToDelete); 
                currentString = currentString.slice(0, -numToDelete); 
                operationHistory.push({ type: 2, value: deletedString }); 
            } else if (type === 3) { // Print operation
                const index = parseInt(opArr[1]) - 1; 
                console.log(currentString[index]);
    
            } else if (type === 4) { 
                const lastOperation = operationHistory.pop();
                if (lastOperation.type === 1) { // Undo append
                    const appendedString = lastOperation.value;
                    currentString = currentString.slice(0, -appendedString.length); 
                } else if (lastOperation.type === 2) { // Undo delete
                    const deletedString = lastOperation.value;
                    currentString += deletedString;
                }
            }
        }
    }
    
  • + 0 comments

    ez in python

    class strEditor :
        def __init__(self):
            self.setting = []
            self.temp = []
        def createSetting(self):
            for s in [input() for _ in range(int(input()))]:
                main = s.split(" ")
                code = main[0]
                command = None
                if len(main) >1:
                    command = main[1] 
                self.setting.append({"code":code,"command":command})
            return self.setting
    
        def operationMethod(self,code, command, finalS,idx):
            if int(code) == 1:
                self.temp.append(finalS)
                finalS+=(command)
            elif int(code) == 2:
                if 0 <= int(command) <= len(finalS):
                    self.temp.append(finalS)
                    finalS = finalS[:-int(command)] 
            elif int(code) == 3:
                if 1 <= int(command) <= len(finalS):
                    print(finalS[int(command)-1])
            elif int(code) == 4:
                finalS = self.temp.pop()
            else:
                print("Invalid operation code.")
            return finalS
            
        def runCommand(self):
            finalS = ''
            for idx in range(len(self.setting)):
                payloads = self.setting[idx]
                code = payloads["code"]
                command = payloads["command"]
                finalS = self.operationMethod(code, command, finalS,idx)
            return finalS
            
    editor = strEditor()
    editor.createSetting()
    editor.runCommand()
    
  • + 0 comments
    x = ['']
    for s in [input() for _ in range(int(input()))]:
        x.append(x[-1]+s[2:]) if s[0] == '1' else x.append(x[-1][:-int(s[2:])]) if s[0] == '2' else print(x[-1][int(s[2:])-1]) if s[0] == '3' else x.pop() if s == '4' else ...
    
  • + 0 comments

    Here is HackerRank Simple Text Editor problem solution in Python, Java, C++, C and javascript