Design a Task Queue
Keep tasks in arrival order with a deque, and return a sorted priority view without changing that order.
The problem
Implement the TaskQueue class. TaskQueue() creates an empty queue. enqueue(name, priority) adds a task to the back. peek() returns the name of the task at the front (the oldest one) without removing it, or None if the queue is empty. dequeue() removes the front task and returns its name, or None if the queue is empty. prioritize() returns every queued task as a [name, priority] list, ordered from the highest priority to the lowest, with names in alphabetical order when priorities are equal. prioritize() must not change the queue: dequeue() still returns tasks in the order they arrived. Names are strings and priorities are integers.
Starter code
from collections import deque
class TaskQueue:
def __init__(self):
pass
def enqueue(self, name, priority):
pass
def peek(self):
pass
def dequeue(self):
pass
def prioritize(self):
passPublic examples
obj = TaskQueue() obj.enqueue("write-tests", 2) obj.enqueue("fix-production", 9) obj.peek() obj.prioritize() obj.dequeue() obj.peek()gives[None, None, None, "write-tests", [["fix-production", 9], ["write-tests", 2]], "write-tests", "fix-production"]obj = TaskQueue() obj.peek() obj.dequeue() obj.prioritize()gives[None, None, None, []]
Solving it in the practice book needs an active paid plan.
Open it in the practice book