Keys versus Values - codepath/compsci_guides GitHub Wiki
Unit 2 Session 1 (Click for link to problem statements)
U-nderstand
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Will both the keys and values always be integers?
- Yes.
P-lan
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Find the sum of all keys, the sum of all values, then print a result based on their comparison.
1) Find the sum of all keys
a) Create a variable to hold the sum of the keys
b) Iterate over all keys as a list
c) Add each key to the sum variable
2) Find the sum of all values in the same way
3) Check which is larger, and print the result accordingly
I-mplement
def keys_v_values(dictionary):
key_sum = 0
value_sum = 0
# Calculate the sum of all keys
for key in dictionary.keys():
key_sum += key
# Calculate the sum of all values
for val in dictionary.values()
value_sum += val
# Determine which sum is greater or if they are equal
if key_sum > value_sum:
return "keys"
elif value_sum > key_sum:
return "values"
else:
return "balanced"