Part 1: Sum all integer values in a JSON object.
def js_sum(js):
if isinstance(js, dict):
return sum(map(js_sum, js.values()))
elif isinstance(js, list):
return sum(map(js_sum, js))
elif isinstance(js, int):
return js
else:
return 0
print(js_sum(json.load(sys.stdin)))
Due to the nature of JSON, this is very much a recursive problem. There’s really not much else to say. If you have a complex data structure (dict
or list
), recur into it. Otherwise, if it’s a number add it, if not don’t.
Part 2: Do not recur into any dict
that contains the value "red"
.
Given the structure we already had, the tweak for this is straight forward (if not terribly efficient):
def js_sum(js):
if isinstance(js, dict) and not 'red' in js.values():
return sum(map(js_sum, js.values()))
elif isinstance(js, list):
return sum(map(js_sum, js))
elif isinstance(js, int):
return js
else:
return 0
print(js_sum(json.load(sys.stdin)))