Codepath

Filter Records by Waste Threshold

Unit 4 Session 2 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 goal of the problem?
    • A: The goal is to filter a list of food waste records and return only those where the waste amount is greater than or equal to a given threshold.
  • Q: What are the inputs?
    • A: The input is a list of tuples where each tuple consists of a date (as a string in the format "YYYY-MM-DD") and an integer representing the amount of food waste on that date, and an integer threshold.
  • Q: What are the outputs?
    • A: The output is a list of tuples that meet or exceed the waste threshold.
  • Q: How should the filtering be done?
    • A: Iterate through the list of records and include only those records where the waste amount is greater than or equal to the threshold.
  • Q: Are there any assumptions about the input?
    • A: The list is well-formed, with valid date strings and non-negative integer values for waste amounts.

P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Iterate over the list of waste records, checking each record against the threshold. If the waste amount meets or exceeds the threshold, add it to the filtered list.

1) Initialize an empty list `filtered_records` to store the records that meet the threshold.
2) Iterate over each `record` in `waste_records`:
   a) If the waste amount in the record is greater than or equal to the `threshold`, add the record to `filtered_records`.
3) Return `filtered_records`.

**⚠️ Common Mistakes**

- Forgetting to compare the waste amount correctly, which could result in filtering out valid records.
- Not preserving the order of the records when filtering, although this is naturally handled by the iteration.
- Assuming that the input might contain no records that meet the threshold, which should result in an empty list being returned.

I-mplement

def filter_records_by_waste_threshold(waste_records, threshold):
    # Initialize an empty list to store filtered records
    filtered_records = []

    # Iterate over the waste records
    for record in waste_records:
        # Check if the waste amount meets or exceeds the threshold
        if record[1] >= threshold:
            filtered_records.append(record)

    return filtered_records
Example Usage:

waste_records1 = [
    ("2024-08-01", 150),
    ("2024-08-02", 200),
    ("2024-08-03", 50),
    ("2024-08-04", 300),
    ("2024-08-05", 100),
    ("2024-08-06", 250)
]
threshold1 = 150

result = filter_records_by_waste_threshold(waste_records1, threshold1)
print(result)  
# Output: [('2024-08-01', 150), ('2024-08-02', 200), ('2024-08-04', 300), ('2024-08-06', 250)]

waste_records2 = [
    ("2024-07-01", 90),
    ("2024-07-02", 120),
    ("2024-07-03", 80),
    ("2024-07-04", 130),
    ("2024-07-05", 70)
]
threshold2 = 100

result = filter_records_by_waste_threshold(waste_records2, threshold2)
print(result)  
# Output: [('2024-07-02', 120), ('2024-07-04', 130)]
Fork me on GitHub