# server/tasks/task_hard.py TASK_HARD = { "task_id": "task_hard", "difficulty": "hard", "num_tests": 10, "description": ( "Fix 3+ subtle bugs in the `flatten_nested_dict` function. " "It should recursively flatten a nested dictionary using dot-notation keys. " "Example: {'a': {'b': 1}} → {'a.b': 1}. " "Bug 1: wrong separator used in recursive calls. " "Bug 2: lists are not handled correctly (should be indexed like 'a.0', 'a.1'). " "Bug 3: empty dict values should produce the parent key with empty dict, not be skipped. " "Bug 4: non-string keys are not converted to strings." ), # The broken version the agent sees "buggy_code": """\ def flatten_nested_dict(d: dict, parent_key: str = '', sep: str = '.') -> dict: \"\"\"Flatten a nested dict into a single-level dict with dot-notation keys. Lists should be expanded with numeric indices: {'a': [1,2]} → {'a.0': 1, 'a.1': 2} Empty dicts should map to {}: {'a': {}} → {'a': {}} Non-string keys should be converted to strings.\"\"\" items = [] for k, v in d.items(): # BUG 4: k not converted to str new_key = parent_key + sep + k if parent_key else k if isinstance(v, dict): # BUG 3: empty dict case not handled — this skips it entirely # BUG 1: passes '/' instead of sep in recursive call items.extend(flatten_nested_dict(v, new_key, '/').items()) elif isinstance(v, list): # BUG 2: uses 1-based indexing instead of 0-based for i, item in enumerate(v, start=1): list_key = new_key + sep + str(i) if isinstance(item, dict): items.extend(flatten_nested_dict(item, list_key, sep).items()) else: items.append((list_key, item)) else: items.append((new_key, v)) return dict(items) """, "clean_code": """\ def flatten_nested_dict(d: dict, parent_key: str = '', sep: str = '.') -> dict: \"\"\"Flatten a nested dict into a single-level dict with dot-notation keys. Lists should be expanded with numeric indices: {'a': [1,2]} → {'a.0': 1, 'a.1': 2} Empty dicts should map to {}: {'a': {}} → {'a': {}} Non-string keys should be converted to strings.\"\"\" items = [] for k, v in d.items(): str_k = str(k) new_key = parent_key + sep + str_k if parent_key else str_k if isinstance(v, dict): if not v: items.append((new_key, {})) else: items.extend(flatten_nested_dict(v, new_key, sep).items()) elif isinstance(v, list): for i, item in enumerate(v): list_key = new_key + sep + str(i) if isinstance(item, dict): items.extend(flatten_nested_dict(item, list_key, sep).items()) else: items.append((list_key, item)) else: items.append((new_key, v)) return dict(items) """, "test_suite": """\ def test_simple_flat(): assert flatten_nested_dict({"a": 1, "b": 2}) == {"a": 1, "b": 2} def test_one_level_nesting(): assert flatten_nested_dict({"a": {"b": 1}}) == {"a.b": 1} def test_deep_nesting(): assert flatten_nested_dict({"a": {"b": {"c": 3}}}) == {"a.b.c": 3} def test_mixed_nesting(): result = flatten_nested_dict({"a": 1, "b": {"c": 2, "d": {"e": 3}}}) assert result == {"a": 1, "b.c": 2, "b.d.e": 3} def test_list_values(): assert flatten_nested_dict({"a": [10, 20, 30]}) == {"a.0": 10, "a.1": 20, "a.2": 30} def test_nested_list_of_dicts(): inp = {"users": [{"name": "Alice"}, {"name": "Bob"}]} expected = {"users.0.name": "Alice", "users.1.name": "Bob"} assert flatten_nested_dict(inp) == expected def test_empty_dict_value(): assert flatten_nested_dict({"a": {}, "b": 1}) == {"a": {}, "b": 1} def test_empty_input(): assert flatten_nested_dict({}) == {} def test_numeric_keys(): assert flatten_nested_dict({1: "a", 2: {"3": "b"}}) == {"1": "a", "2.3": "b"} def test_complex_mixed(): inp = {"x": [{"y": [1, 2]}, {"z": 3}], "w": 4} expected = {"x.0.y.0": 1, "x.0.y.1": 2, "x.1.z": 3, "w": 4} assert flatten_nested_dict(inp) == expected """, }