Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for YAML. Simple Dict by book1978
def yaml(a: str) -> dict:
result = {}
for line in a.splitlines():
line = line.strip()
if line:
key, value = map(str.strip, line.split(":"))
result[key] = int(value) if value.isdigit() else value
return result
print("Example:")
print(
yaml(
"""name: Alex
age: 12"""
)
)
# These "asserts" are used for self-checking
assert yaml("name: Alex\nage: 12") == {"name": "Alex", "age": 12}
assert yaml("name: Alex Fox\nage: 12\n\nclass: 12b") == {
"age": 12,
"name": "Alex Fox",
"class": "12b",
}
print("The mission is done! Click 'Check Solution' to earn rewards!")
June 20, 2023
Comments: