question 168 Nested Data Structures · medium
Flatten the arrow-shaped lookups
summary works, but every value is guarded by three nested ifs, so the code drifts right like an arrow and is hard to change. Refactor it with get so it reads straight down. Behaviour must not change, including the original's handling of a value stored as None (it falls back to the default, exactly like a missing key).
Function
summary(feature) (same name, same result)Returns
"<alert> alert, felt by <felt>", with "green" and 0 when the alert or felt value is missing or NoneConstraintat most two
if statements in your whole programExamples
callsummary({'id': 'us6000pw8f', 'properties': {'mag': 4.6, 'felt': 1, 'alert': None}, 'geometry': {'coordinates': [122.5991, 8.8699, 10]}})returns'green alert, felt by 1'
callsummary({'id': 'us7000p001', 'properties': {'mag': 6.1, 'alert': 'yellow', 'felt': 312}})returns'yellow alert, felt by 312'
callsummary({'id': 'us6000pw77', 'properties': {'mag': 4.7}})returns'green alert, felt by 0'