In this lesson, you’ll use the collections.deque
data structure to create a ticketing queue. Deques allow for appending to both left and right and popping from both left and right in constant time. Here’s an example:
>>> from collections import deque
>>> deq = deque([1, 2, 3])
>>> deq.appendleft(5)
>>> deq.append(6)
>>> deq
deque([5, 1, 2, 3, 6])
>>> deq.popleft()
5
>>> deq.pop()
6
>>> deq
deque([1, 2, 3])
You can read more about deque
in the documentation for the Python collections module.
Max on April 24, 2020
Hey James, thanks for this outstanding series, it’s actually way more than a help for an interview - it’s an overview of a lot of cool tips and tricks.
However, in this example I think that running the doctest with these expected outputs doesn’t quite cut it, especially for
add_person
andbypass_queue
. If I understand that correctly, the return string of those methods is only based on the input name, not on what happens to the list/deque. Maybe checking for the actual state of the deque could provide a better solution? But maybe I’m just a bit nitpicky here…Whatever, the series is great, learned a lot of things I can expand on now.
Best, Max