Codepath

Track Waste Reduction Trends

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 determine if there is a trend of reducing food waste over time, meaning that each subsequent day has a lower amount of food waste than the previous day.
  • Q: What are the inputs?
    • A: The input is a sorted list of tuples, where each tuple contains a date (as a string in the format "YYYY-MM-DD") and an integer representing the amount of food wasted on that date.
  • Q: What are the outputs?
    • A: The output is a boolean value: True if the food waste decreases every day compared to the previous day, and False otherwise.
  • Q: How should the trend be evaluated?
    • A: Iterate through the list and check if each day's waste amount is less than the previous day's waste amount. If any day does not follow this trend, return False.
  • Q: Are there any assumptions about the input?
    • A: The list is sorted by date and contains valid date strings and non-negative integer values for waste amounts.

P-lan

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.

I-mplement

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
Fork me on GitHub