easy +8 pts

Parse environment variables

Turn an env-style string into a clean dictionary with proper types and fallbacks.

Write a function `parse_env_vars(env_string: str) -> dict` that parses a multi-line environment variable block into a dictionary. Each non-empty line is either a comment (starts with `#`) or a `KEY=VALUE` assignment. Trim leading/trailing whitespace from each line; ignore comments and empty lines. For assignments: - Split on the first `=` only, so values may contain `=`. - Strip whitespace from both key and value. - Keys are case-sensitive; if a key appears more than once, the last occurrence wins. - Convert the value to the most appropriate type: - `'true'` → `True`, `'false'` → `False` (case-insensitive) - an integer (e.g., `'42'`) → `int` - a float (e.g., `'3.14'`) → `float` - otherwise keep the string as-is. Return the resulting dictionary.

Constraints

- `env_string` length ≤ 10^5. - Number of lines ≤ 10^4. - Keys only contain letters, digits, and underscores. - Values are assigned as strings; no nested quotes. - Complexity: O(n) time, O(k) space where k is number of assignments.

Example

>>> parse_env_vars("# comment\nPORT=8080\nDEBUG=true\nRATIO=3.14\nNAME=My App=1")
{'PORT': 8080, 'DEBUG': True, 'RATIO': 3.14, 'NAME': 'My App=1'}
>>> parse_env_vars("EMPTY=\nZERO=0")
{'EMPTY': '', 'ZERO': 0}
>>> parse_env_vars("")
{}
8 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Iterate over lines and skip lines that start with '#' after stripping.
Use `split('=', 1)` to handle values containing '='.
Write a small helper to convert the value string to bool/int/float/str.
Remember to update the dictionary for duplicates so the last wins.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.