- Notifications
You must be signed in to change notification settings - Fork 266
Sum Even Values
kyra-ptn edited this page Aug 15, 2025 · 5 revisions
Unit 2 Session 1 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Will all dictionary values be integers?
- Yes.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Given a dictionary with all-integer values, add up all values that are even.
1) Sum variable starts at 0 2) For each value in the dictionary's values: a) If the value is even, add it to the sum 3) Return the sum
- You might be tempted to write a more complex loop using keys, but Python provides a way to get a list of just the
values
for the dictionary.
def sum_even_values(dictionary): sum_even = 0 for value in dictionary.values(): if value % 2 == 0: sum_even += value return sum_even
Alternative Solution: Use bracket notation to access the dictionary's value
def sum_even_values(dictionary): sum_even = 0 for key in dictionary: if dictionary[key] % 2 == 0: sum_even += dictionary[key] return sum_even