Skip to content

Supply Chain

Jessica Sang edited this page Sep 14, 2024 · 1 revision

Unit 4 Session 1 Advanced (Click for link to problem statements)

U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Q: What is the structure of the input?

    • A: The input is a list of tuples, where each tuple contains a supply name (string) and its priority level (integer).
  • Q: What is the output?

    • A: The output is a list of supply names in the order they would be processed, with higher priority supplies processed first.
  • Q: How should the supplies be processed?

    • A: The supplies should be processed based on their priority, with the highest priority supplies processed first.
  • Q: Are there any constraints on the input, such as the supplies needing to be pre-sorted?

    • A: The supplies do not need to be pre-sorted; the function will handle sorting by priority.

P-lan

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).

I-mplement

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
Clone this wiki locally