We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Practice
  • Certification
  • Compete
  • Career Fair
  • Hiring developers?
  1. Practice
  2. Data Structures
  3. Stacks
  4. Simple Text Editor
  5. Discussions

Simple Text Editor

Problem
Submissions
Leaderboard
Discussions

    You are viewing a single comment's thread. Return to all comments →

  • Python0222 4 years ago+ 0 comments
    class Stack:
        def __init__(self):
            self.name=''
            self.items=['']
            
        def push(self, char):
            self.items.append(self.name+char)
            return self.name+char
        
        def delete(self, k):
            self.name=self.name[:-k]
            self.items.append(self.name)
            
        def undo(self):
            self.items.pop()
            self.name=self.items[-1]
        
        def is_empty(self):
            return self.name == ''
    
    Q=int(input().strip())
    string = Stack()
    for _ in range(Q):
        x= input().split()
        if x[0]=='1':
            string.name=string.push(x[1])
        elif x[0]=='2':
            string.delete(int(x[1]))
        elif x[0]=='3':
            print(string.name[int(x[1])-1])
        elif x[0]=='4':
            string.undo()
    
    2|
    Permalink
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature