Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for The Flat Dictionary by hirunners
def flatten(dictionary: dict[str, str | dict]) -> dict[str, str]:
# your code here
def searchbelow(dictionary):
newdict={}
for key in dictionary:
if type(dictionary[key])==type(str()):
newdict.update({key:dictionary[key]})
else:
if dictionary[key]=={}:
newdict.update({key:""})
else:
newdict.update({key+'/'+k:v for k,v in searchbelow(dictionary[key]).items()})
pass
return newdict
return searchbelow(dictionary)
print("Example:")
print(flatten({"key": "value"}))
# These "asserts" are used for self-checking
assert flatten({"key": "value"}) == {"key": "value"}
assert flatten({"key": {"deeper": {"more": {"enough": "value"}}}}) == {
"key/deeper/more/enough": "value"
}
assert flatten({"empty": {}}) == {"empty": ""}
assert flatten(
{
"name": {"first": "One", "last": "Drone"},
"job": "scout",
"recent": {},
"additional": {"place": {"zone": "1", "cell": "2"}},
}
) == {
"name/first": "One",
"name/last": "Drone",
"job": "scout",
"recent": "",
"additional/place/zone": "1",
"additional/place/cell": "2",
}
print("The mission is done! Click 'Check Solution' to earn rewards!")
Feb. 11, 2024
Comments: