
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.
- Keep the line in a
deque.joinusesappend,vipusesappendleft, andboardusespopleft. - Keep a
historylist as a stack. Push the name each time someone joins or is a vip. - For
undo, pop names offhistoryuntil you find one that is still in the line, and remove that name from the line. - If the line is empty,
boarddoes nothing. If there is nothing to undo,undodoes nothing.
Complete run_dock(commands) and return the list of names in the
order they boarded.
- Keep the line in a
deque.joinusesappend,vipusesappendleft, andboardusespopleft. - Keep a
historylist as a stack. Push the name each time someone joins or is a vip. - For
undo, pop names offhistoryuntil you find one that is still in the line, and remove that name from the line. - If the line is empty,
boarddoes nothing. If there is nothing to undo,undodoes nothing.
main.py Python sleeping
Press Run to try your code, then Check to test it.