medium +20 pts

Serialize dict to TOML-like

Convert a nested Python dict into a simplified TOML-like string with sorted keys and flat table sections.

Implement a function `to_toml(data: dict) -> str` that converts a Python dictionary into a simplified TOML-like string using the following rules: - All keys at every level must be sorted alphabetically (case-sensitive, standard Python string order) before serialization. - Top-level scalar keys and values are written as `key = value` lines. - String values must be double-quoted using Python's `json.dumps` style quoting (to escape quotes and backslashes). - Integers and floats are written as-is using `repr` (e.g., `42`, `3.14`). - Booleans are `true` or `false` (lowercase). - Lists of simple values (str, int, float, bool) are written as a TOML array: `[1, 2, 3]` or `["a", "b"]`. Strings inside lists must be double-quoted, numbers are as `repr`. - Nested dicts become tables. A table header `[key]` (or dotted key path like `[parent.sub]`) is written on its own line, followed by its key-value lines. Important: when inside a table, sub-keys are written WITHOUT any parent prefix, using only their local key name. For example, inside `[a.b]`, a key `c` is written as `c = 1`, not `a.b.c = 1`. - Nested dicts inside a list of dicts are serialized as arrays of tables. Each element becomes its own `[[key]]` section. Inside such a section, keys are written with their local names only (no prefix). If an element dict contains nested dicts, those become subtables with headers like `[[key.sub]]` and inside them keys are also local-only. - **Empty dicts are written as `key = {}` on a single line at their level. Empty lists are written as `key = []` on a single line.** - All lines end with a newline `\n`. No trailing blank lines. Examples of value formatting: - `"hello"` → `"hello"` - `'it\'s'` → `"it's"` - `42` → `42` - `3.14` → `3.14` - `True` → `true` - `[1, "two", True]` → `[1, "two", true]` For nested structures, follow this ordering: parent table headers come before their subtables / arrays of tables. Within a table, write the scalar key-value lines first (sorted), then the subtables (sorted by key), then arrays of tables (sorted by key). When writing keys inside a table, always use the local key name only. Example: if `data = {'a': {'b': {'c': 1}}}`, output is: ``` [a] [a.b] c = 1 ``` Another example: `data = {'servers': [{'host': 'a', 'port': 80}, {'host': 'b', 'port': 8080}]}` becomes: ``` [[servers]] host = "a" port = 80 [[servers]] host = "b" port = 8080 ``` Assume input will be a dict with keys that are valid TOML bare keys (alphanumeric, dash, underscore). Values can be str, int, float, bool, dict, list of simple values, or list of dicts. None is not allowed. Lists of simple values are flat (no nested lists/dicts). Lists of dicts are handled as arrays of tables. Write your function with the signature `to_toml(data: dict) -> str`.

Constraints

Input dictionary depth is at most 10. Number of keys in any single dict is at most 100. Values can be str, int, float, bool, dict, list, or list of dicts. Lists of simple values are flat (no nested lists/dicts). All keys are non-empty strings. The final string should not exceed 10,000 characters.

Example

```python
>>> to_toml({'name': 'Ada', 'age': 37})
'age = 37\nname = "Ada"\n'

>>> to_toml({'list': [1, 'two', True], 'nested': {'x': 1}})
'list = [1, "two", true]\n[nested]\nx = 1\n'

>>> to_toml({'servers': [{'host': 'a', 'port': 80}, {'host': 'b', 'port': 8080}]})
'[[servers]]\nhost = "a"\nport = 80\n[[servers]]\nhost = "b"\nport = 8080\n'

>>> to_toml({'empty_list': [], 'empty_dict': {}})
'empty_dict = {}\nempty_list = []\n'
```
20 points ~25 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use a helper that writes a table given a dict and a current key path. Inside that helper, first write scalar key-value lines (using the local key name only), then recurse into nested dicts and arrays of tables.
For each dict, separate keys into three groups: scalar values, dict values, and list-of-dicts values. Sort each group by key.
For arrays of tables, iterate over the list, emit a `[[path]]` header, then call the table-writing helper for each element with the same path. Inside, use local key names only.
Empty dicts and empty lists are treated as scalar values: write them inline as `key = {}` and `key = []` respectively.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.