#602 · Dictionaries and sets · Intermediate · Review the AI's code · 70 XP · about 10 min
Someone asked an AI assistant: "Write a function that looks up a value in a nested dictionary using a dotted path like 'db.port', and returns a default if it is missing." This is its answer. It looks right, but it has a bug.
App settings are often stored as dictionaries inside dictionaries. get_setting(config, path, default=None) should follow path, a string of keys joined by dots, one level at a time, and return the value it ends at. If any key along the way is missing, or the path tries to go inside something that is not a dictionary, it returns default instead.
config = {'db': {'host': 'localhost', 'port': 5432}, 'debug': False} get_setting(config, 'db.port') # returns 5432 get_setting(config, 'debug') # returns False get_setting(config, 'db.user', 'admin') # returns 'admin' get_setting(config, 'cache.size', 64) # returns 64
path has 1 to 10 keys; keys never contain dots.First solves only, ranked by stars and then time.