easy +10 pts

Parse YAML-like dict

Convert a simple indented YAML-like format into a nested dictionary.

Write a function `parse_yaml(yaml_text: str) -> dict` that parses a simplified YAML-like format. The input is a string with lines like `key: value` or `key:` followed by indented subkeys. Indentation is exactly two spaces per level. Values are simple strings (no quotes, no special characters). Lines with a key and no value indicate a nested dictionary. Blank lines are ignored. Return the fully nested dictionary. In case of duplicate keys at the same level, keep the last one.

Constraints

The input will contain at most 50 lines and each line length ≤ 100 characters. Values contain no ':' or leading/trailing spaces. The top-level keys are unindented. Assume valid format and consistent indentation.

Example

>>> parse_yaml("name: Alice\nage: 30")
{'name': 'Alice', 'age': '30'}
>>> parse_yaml("root:\n  child: 5")
{'root': {'child': '5'}}
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Track the current nesting level and build dictionaries from root.
Use a stack (list) to remember the path of dictionaries as you go deeper.
For a line without ':', it just adds to the current dictionary; for a line with ':', it starts a new nested dictionary.
Remember to handle blank lines gracefully.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.