From the course: Faster Python Code

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

heapq

heapq - Python Tutorial

From the course: Faster Python Code

Start my 1-month free trial

heapq

- [Presenter] In some cases we'd like to order our tasks by priority. This is known as a priority queue. Our initial implementation will get a task and a priority, add them to the list, and then sort the list by priority. This way, when we pop the next task to execute, we'll get the one with the highest priority. Priority is given in decreasing numbers, meaning priority one is higher than three. If we look at the code, at line six we have the priority queue, and we create in line nine the tasks, which is a list. And every time we push some task with a priority, we create a topple with the priority and the task and then sort it in line 13. When we pop, we pop the first item on the list and get item number one from it which is the task to execute. In line 22 we add a test, the test's a priority queue. And we also have a benchmark. In line 49, we have a function called gen_cases, which generate test cases for us. And in…

Contents