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.
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.
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}