Unit 4 Session 1 Advanced (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
Q: What is the structure of the input?
Q: What is the output?
Q: How should the supplies be processed?
Q: Are there any constraints on the input, such as the supplies needing to be pre-sorted?
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Sort the supplies by their priority in descending order, then process them in that order.
1) Sort the `supplies` list by the priority level in descending order.
2) Initialize a `deque` to store the supply names in the sorted order.
3) For each `supply` in `supplies`:
a) Enqueue the `supply[0]` (supply name) into the `deque`.
4) Initialize an empty list called `processed_supplies`.
5) While the `deque` is not empty:
a) Dequeue the first element from the `deque` and append it to `processed_supplies`.
6) Return the `processed_supplies` list.
**⚠️ Common Mistakes**
- Forgetting to correctly sort the supplies by their priority level.
- Mismanaging the deque operations (enqueue and dequeue).
from collections import deque
def process_supplies(supplies):
supplies.sort(key=lambda x: -x[1]) # Sort supplies by priority in descending order
queue = deque(supply[0] for supply in supplies) # Enqueue the supplies in sorted order
processed_supplies = []
while queue:
processed_supplies.append(queue.popleft()) # Process supplies in the order they are dequeued
return processed_supplies