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.
# Enter your code here. Read input from STDIN. Print output to STDOUTclassEditor:def__init__(self,s,verbose=False):self.s=sself._s_history=[]self.verbose=verbosedefappend(self,w:str,save=True):self.s=self.s+wifsave:self._s_history.append(('1',w))ifself.verbose:print(f'append:{w};history:{self._s_history}')defdelete(self,k:int,save=True):delta=self.s[-1*k:]ifsave:self._s_history.append(('2',delta))self.s=self.s[:-1*k]ifself.verbose:print(f'delete:{delta};history:{self._s_history}')defprint(self,k:int):'''printthek`thcharofself.s'''print(self.s[k-1])defundo(self):'''undothelastoperation'''query,delta=self._s_history.pop()ifquery=='1':self.delete(len(delta),save=False)ifquery=='2':self.append(delta,save=False)defexecute_raw_query(self,raw_query:str):'''parseandexecutequery'''args=Nonequery=raw_query.split()iflen(query)==1:query=query[0]else:query,args=queryifquery=='1':returnself.append(args)ifquery=='2':returnself.delete(int(args))ifquery=='3':returnself.print(int(args))ifquery=='4':returnself.undo()returnNoneif__name__=='__main__':q=int(input())initial_string=''editor=Editor(initial_string)for_inrange(q):raw_query=input()editor.execute_raw_query(raw_query)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Simple Text Editor
You are viewing a single comment's thread. Return to all comments →