📖 Scenario: Imagine you are managing a hospital emergency room. Patients arrive with different levels of urgency. You want to treat the most urgent patients first. This is a perfect use case for a priority queue.
🎯 Goal: You will create a simple priority queue using a list of tuples where each tuple contains a patient's name and their priority level. Then, you will extract patients in order of their priority.
📋 What You'll Learn
Create a list called patients with given patient names and priority levels
Create a variable called highest_priority to track the highest priority found
Write a loop to find the patient with the highest priority
Print the patient with the highest priority
💡 Why This Matters
🌍 Real World
Priority queues are used in hospitals, task scheduling, and anywhere urgent tasks must be handled first.
💼 Career
Understanding priority queues helps in roles like software development, system design, and operations where task prioritization is key.
Progress0 / 4 steps
1
Create the patient list
Create a list called patients with these exact entries: ("Alice", 2), ("Bob", 5), ("Charlie", 1), ("Diana", 4)
DSA Python
Hint
Use a list of tuples where each tuple has a name and a number for priority.
2
Set up the highest priority variable
Create a variable called highest_priority and set it to 0 to start tracking the highest priority
DSA Python
Hint
Start with 0 because priorities are positive numbers and we want to find the maximum.
3
Find the patient with the highest priority
Use a for loop with variables name and priority to iterate over patients. Inside the loop, update highest_priority if priority is greater than highest_priority. Also, create a variable called top_patient to store the name of the patient with the highest priority.
DSA Python
Hint
Compare each patient's priority to the current highest and update both the highest priority and the top patient name.
4
Print the patient with the highest priority
Write a print statement to display the text: "Patient with highest priority: " followed by the value of top_patient
DSA Python
Hint
Use string concatenation or f-string to combine the message and the patient name.
Practice
(1/5)
1. What is the main feature of a priority queue compared to a regular queue?
easy
A. It does not allow insertion after creation
B. Elements are always dequeued in the order they were inserted
C. It only stores elements of the same value
D. Elements are dequeued based on their priority, not just order of insertion
Solution
Step 1: Understand queue behavior
A regular queue removes elements in the order they were added (FIFO).
Step 2: Understand priority queue behavior
A priority queue removes elements based on priority, not just order.
Final Answer:
Elements are dequeued based on their priority, not just order of insertion -> Option D
Quick Check:
Priority queue = priority-based removal [OK]
Hint: Priority queue removes highest priority first, not oldest [OK]
Common Mistakes:
Confusing priority queue with FIFO queue
Thinking priority queue only stores equal values
Assuming no insertion allowed after creation
2. Which of the following is the correct way to import and create a min-priority queue using Python's heapq module?
easy
A. from queue import PriorityQueue; pq = PriorityQueue()
B. import heapq; pq = []
C. import heapq; pq = heapq.PriorityQueue()
D. import queue; pq = queue.heapq()
Solution
Step 1: Identify correct module for heapq min-priority queue
Python's heapq module implements min-heap using lists.
Step 2: Check import and creation syntax
import heapq; pq = [] correctly imports heapq and creates an empty list to use as heap.
Final Answer:
import heapq; pq = [] -> Option B
Quick Check:
heapq module with empty list = correct [OK]
Hint: heapq uses empty list [] for min-heap [OK]
Common Mistakes:
Confusing heapq (functions) with PriorityQueue class
Trying to call heapq.PriorityQueue() which doesn't exist