Push tasks with priorities and always get the most urgent one first, using Python's heapq.
Python · intermediate · 5 min · 8 steps · free
import heapq
tasks = []
heapq.heappush(tasks, (3, 'write tests'))
heapq.heappush(tasks, (1, 'fix bug'))
heapq.heappush(tasks, (2, 'review'))
first = heapq.heappop(tasks)
second = heapq.heappop(tasks)
print(first, second, tasks)heapq module, so the program can use the functions it provides.tasks is created and points to a new list: [].tasks points to.second is created and points to a new tuple: [2, 'review']. a tuple is removed from the start of the list that tasks points to.(1, 'fix bug') (2, 'review') [(3, 'write tests')].Answer: The smallest item
Answer: Push negative priorities
← Binary Search Trees · Breadth-First Search →