Unit 4 Session 2 Advanced (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
"YYYY-MM-DD"
) and an integer representing the amount of food waste on that date, and an integer threshold.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.
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)]