Question 5
Given the following code snippet, what will be the output?
def update_records(records, updates):
for key, value in updates.items():
if key in records and isinstance(records[key], dict):
for sub_key, sub_val in value.items():
if sub_key in records[key]:
records[key][sub_key] += sub_val
else:
records[key][sub_key] = sub_val
else:
records[key] = value
return records
data = {
"A": {"x": 10, "y": 20},
"B": {"x": 5, "z": 15},
"C": 100
}
updates = {
"A": {"x": 5, "z": 5},
"B": {"y": 10},
"C": {"x": 10},
"D": {"w": 7}
}
result = update_records(data, updates)
print(result)
A: {'x': 15, 'y': 20, 'z': 5}
B: {'x': 5, 'z': 15, 'y': 10}
C: {'x': 10}
D: {'w': 7}A: {'x': 15, 'y': 20, 'z': 5}
B: {'x': 5, 'z': 15, 'y': 10}
C: {'x': 110}
D: {'w': 7}A: {'x': 5, 'z': 5}
B: {'y': 10}
C: {'x': 10}
D: {'w': 7}A: {'x': 5, 'y': 20, 'z': 5}
B: {'x': 5, 'z': 15, 'y': 10}
C: 100
D: {'w': 7}