Queues
Python · Unit 24: Data structures
- Full plan
- Python
- 9 exercises
A queue is the line at the bank: the first to arrive is the first to be served.
collections brings deque, built for this: append joins the back and popleft serves the one at the front.
from collections import deque
queue = deque(["Amy", "Leo"])
queue.append("Sam")
print(queue.popleft())Prints
Amy
The rest of the explanation is in the lesson, which is part of the full plan.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Predict the output
What does this code print?
2. Complete the code
Complete the import. It should print: Amy
3. Predict the output
What does this code print?
4. Predict the output
What does this code print?
5. Find the bug
It should serve whoever has waited the longest. Which line has the error?
6. Multiple choice
What makes a
dequedifferent from a plain list?7. Put the lines in order
Put
serve(names, times)together; it serves the first ones and returns who is still in line.8. Find the case that fails
next_up(names)should return whose turn it is, or the text "nobody" if there's no line. Which call breaks it?9. Predict the output
Max arrives, the next in line gets served and Mia comes in through the emergency door. What does it print?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.