Skip to content

Calculate Weekly Waste Totals

Raymond Chen edited this page Sep 1, 2024 · 1 revision

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 calculate the total food waste for each day of the week and return the results as a dictionary.
  • Q: What are the inputs?
    • A: The input is a dictionary where each key is a day of the week, and the value is a list of integers representing the food waste (in kilograms) recorded on that day.
  • Q: What are the outputs?
    • A: The output is a dictionary where each key is a day of the week, and the value is the total food waste for that day.
  • Q: How should the total waste for each day be calculated?
    • A: For each day, sum the integers in the list associated with that day.
  • Q: Are there any assumptions about the input?
    • A: The dictionary is well-formed, with valid day names as keys and lists of non-negative integers as values.

P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Iterate over each day in the input dictionary, sum the waste amounts for that day, and store the result in a new dictionary.

1) Initialize an empty dictionary `total_waste`. 2) Iterate over each `day, waste_list` pair in the `weekly_waste` dictionary: a) Calculate the sum of the `waste_list`. b) Store the sum in `total_waste` with `day` as the key. 3) Return `total_waste`. **⚠️ Common Mistakes** - Forgetting to correctly sum the values in the waste list, leading to inaccurate totals. - Assuming that all days will have waste records, not handling empty lists or missing days. - Misunderstanding the input format, which could result in incorrect processing of the waste data.

I-mplement

def calculate_weekly_waste_totals(weekly_waste): # Calculate the total waste for each day total_waste = {day: sum(waste) for day, waste in weekly_waste.items()} return total_waste
Example Usage: weekly_waste = { 'Monday': [5, 3, 7], 'Tuesday': [2, 4, 6], 'Wednesday': [8, 1], 'Thursday': [4, 5], 'Friday': [3, 2, 1], 'Saturday': [6], 'Sunday': [1, 2, 2] } print(calculate_weekly_waste_totals(weekly_waste)) # Output: {'Monday': 15, 'Tuesday': 12, 'Wednesday': 9, 'Thursday': 9, 'Friday': 6, 'Saturday': 6, 'Sunday': 5}
Clone this wiki locally