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 wasted on that date.True
if the food waste decreases every day compared to the previous day, and False
otherwise.False
.Plan the solution with appropriate visualizations and pseudocode.
General Idea: Compare the waste amount of each day with the previous day's waste amount. If all comparisons show a decrease, return True
; otherwise, return False
.
1) If the `waste_records` list is empty, return `True` since no trend can be identified.
2) Initialize `previous_waste` to the waste amount of the first record.
3) Iterate through the `waste_records` list starting from the second record:
a) Compare the current waste amount with `previous_waste`.
b) If the current waste amount is not less than `previous_waste`, return `False`.
c) Update `previous_waste` to the current waste amount for the next iteration.
4) If all days show a decrease, return `True`.
**⚠️ Common Mistakes**
- Forgetting to correctly compare the waste amounts, which could result in an incorrect determination of the trend.
- Assuming that the list will always have multiple records, not handling the case of a single record or an empty list.
- Misunderstanding the input format, leading to incorrect processing of the records.
def track_waste_reduction_trends(waste_records):
# Check if there are no records
if not waste_records:
return True # No trend can be identified if there are no records
# Initialize the previous waste amount with the waste of the first record
previous_waste = waste_records[0][1]
# Iterate through the records starting from the second item
for record in waste_records[1:]:
current_waste = record[1]
# If the current waste is not less than the previous waste, trend is not reducing
if current_waste >= previous_waste:
return False
# Update previous waste amount for the next comparison
previous_waste = current_waste
return True
Example Usage:
waste_records_1 = [
("2024-08-01", 150),
("2024-08-02", 120),
("2024-08-03", 100),
("2024-08-04", 80),
("2024-08-05", 60)
]
waste_records_2 = [
("2024-08-01", 150),
("2024-08-02", 180),
("2024-08-03", 150),
("2024-08-04", 140),
("2024-08-05", 120)
]
print(track_waste_reduction_trends(waste_records_1))
# Output: True
print(track_waste_reduction_trends(waste_records_2))
# Output: False