File size: 4,319 Bytes
cacd58c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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
""",
}