You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
defkeys_v_values(dictionary):
key_sum=0value_sum=0# Calculate the sum of all keysforkeyindictionary.keys():
key_sum+=key# Calculate the sum of all valuesforvalindictionary.values():
value_sum+=val# Determine which sum is greater or if they are equalifkey_sum>value_sum:
return"keys"elifvalue_sum>key_sum:
return"values"else:
return"balanced"