Undo and redo

Write code · Medium · §5.1 Stacks

commands holds, in order, the commands given to a text box. The command "type <word>" (for example, "type hi") adds the word to the end of the text, with no space before it. "undo" removes the most recently added word that is still in the text and sets it aside. "redo" takes the word that was set aside most recently and adds it back to the end of the text.

When a new type command is given, all the words set aside are thrown away. If there is no word to remove, undo does nothing, and if no word is set aside, redo does nothing. Complete the function solution, which returns the text after all the commands have been run. If the text is empty, return an empty string "".

Function

solution(commands)

Constraints

Examples

commandsresult
['type good', 'type day', 'undo', 'type night']'goodnight'
['type see', 'type you', 'undo', 'redo', 'type soon']'seeyousoon'

Starter code

def solution(commands):
    answer = ""
    return answer

Solve this problem in your browser

Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.