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.
s=''# Current string statehistory=[]# Stack of undo operations (each is a function that reverts a change)for_inrange(int(input())):command=input().rstrip().split()matchcommand[0]:case'1':# Append operation# Save undo operation (removes the appended part)# Using lambda with default arg to capture current command[1] lengthhistory.append(lambdas,chars_to_remove=len(command[1]):s[:-chars_to_remove])# Perform the appends+=command[1]case'2':# Delete operationchars_to_delete=int(command[1])# Save undo operation (re-adds the deleted portion)# Store the deleted substring as default arg to avoid late binding issueshistory.append(lambdas,deleted_part=s[-chars_to_delete:]:s+deleted_part)# Perform the deletions=s[:-chars_to_delete]case'3':# Print character at indexprint(s[int(command[1])-1])case'4':# Undo last operationifhistory:# Only undo if there's history# Apply the most recent undo operations=history.pop()(s)
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 →