Data structures · 9 / 15 Exercise Hard

Browser history

Objective

Create a Python program that simulates the back and forward buttons of a browser using two stacks.

Instructions

  • The browser starts on the page home. Read an integer n and then n commands.
  • visit PAGE: the current page goes onto the back stack, the forward stack is cleared and Page: PAGE is shown.
  • back: moves to the previous page (the current one goes onto the forward stack) and shows Page: X. If there are no previous pages, show Cannot go back.
  • forward: the opposite operation; if there are no pages ahead, show Cannot go forward.
Input
6
visit a.com
visit b.com
back
back
back
forward
Output
Page: a.com
Page: b.com
Page: a.com
Page: home
Cannot go back
Page: a.com

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise