Checkpoint

Checkpoint: The Ferry Dock

The ferry to Skyreach Isle has one gangway and a long line of travellers. The dockmaster shouts commands, and you keep the line in order. You'll need a queue for the line and a stack to undo mistakes.

Each command is a string:

Command What happens
"join Ada" Ada joins the back of the line
"vip Bo" Bo goes straight to the front
"board" the person at the front boards the ferry
"undo" cancel the most recent join or vip that is still waiting in line

Assignment

Complete run_dock(commands) and return the list of names in the order they boarded.

  1. Keep the line in a deque. join uses append, vip uses appendleft, and board uses popleft.
  2. Keep a history list as a stack. Push the name each time someone joins or is a vip.
  3. For undo, pop names off history until you find one that is still in the line, and remove that name from the line.
  4. If the line is empty, board does nothing. If there is nothing to undo, undo does nothing.