Data structures · 15 / 15 Exercise Hard

Task priority queue

Objective

Create a Python program that manages a priority queue of tasks with the heapq module: the task with the lowest priority number always runs first.

Instructions

  • Read an integer n and then n commands. Names have no spaces and priorities are integers.
  • add PRIORITY NAME: adds the task and shows Added: NAME.
  • run: pops the task with the lowest priority and shows Running: NAME (priority P). On a tie, the one added first goes first (store a counter next to the priority). If there are no tasks, show No tasks.
  • At the end, show Pending: K with the tasks left.
Input
6
add 3 informe
add 1 backup
add 3 correo
run
run
run
Output
Added: informe
Added: backup
Added: correo
Running: backup (priority 1)
Running: informe (priority 3)
Running: correo (priority 3)
Pending: 0

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise