File size: 8,376 Bytes
6217a61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
from typing import List, Dict, Any

def get_challenges() -> List[Dict[str, Any]]:
    """
    Collection of buggy Python challenges.
    Each challenge has:
      - id, difficulty, description
      - buggy_code: the broken code agent must fix
      - error_message: what Python throws
      - correct_output: expected result when fixed
      - hint: optional nudge
    """
    return [

        # ── EASY: Syntax Errors ──────────────────────────────────────

        {
            "id": "easy_001",
            "difficulty": "easy",
            "description": "Fix the function that adds two numbers.",
            "buggy_code": (
                "def add(a, b)\n"
                "    return a + b\n"
                "print(add(3, 4))"
            ),
            "error_message": "SyntaxError: expected ':'",
            "correct_output": "7",
            "hint": "Check the function definition line."
        },
        {
            "id": "easy_002",
            "difficulty": "easy",
            "description": "Fix the loop that prints numbers 1 to 5.",
            "buggy_code": (
                "for i in range(1, 6)\n"
                "    print(i)"
            ),
            "error_message": "SyntaxError: expected ':'",
            "correct_output": "1\n2\n3\n4\n5",
            "hint": "for loops need a colon at the end."
        },
        {
            "id": "easy_003",
            "difficulty": "easy",
            "description": "Fix the function that checks if a number is even.",
            "buggy_code": (
                "def is_even(n):\n"
                "return n % 2 == 0\n"
                "print(is_even(4))"
            ),
            "error_message": "IndentationError: expected an indented block",
            "correct_output": "True",
            "hint": "Check indentation inside the function."
        },

        # ── MEDIUM: Logic Bugs ───────────────────────────────────────

        {
            "id": "medium_001",
            "difficulty": "medium",
            "description": "Fix the function that finds the maximum in a list.",
            "buggy_code": (
                "def find_max(nums):\n"
                "    max_val = 0\n"
                "    for n in nums:\n"
                "        if n > max_val:\n"
                "            max_val = n\n"
                "    return max_val\n"
                "print(find_max([-5, -1, -3]))"
            ),
            "error_message": "No error β€” but wrong output. Expected: -1, Got: 0",
            "correct_output": "-1",
            "hint": "What happens when all numbers are negative?"
        },
        {
            "id": "medium_002",
            "difficulty": "medium",
            "description": "Fix the factorial function.",
            "buggy_code": (
                "def factorial(n):\n"
                "    result = 0\n"
                "    for i in range(1, n + 1):\n"
                "        result *= i\n"
                "    return result\n"
                "print(factorial(5))"
            ),
            "error_message": "No error β€” but wrong output. Expected: 120, Got: 0",
            "correct_output": "120",
            "hint": "Check the initial value of result."
        },
        {
            "id": "medium_003",
            "difficulty": "medium",
            "description": "Fix the function that reverses a string.",
            "buggy_code": (
                "def reverse_string(s):\n"
                "    result = ''\n"
                "    for i in range(len(s)):\n"
                "        result += s[i]\n"
                "    return result\n"
                "print(reverse_string('hello'))"
            ),
            "error_message": "No error β€” but wrong output. Expected: olleh, Got: hello",
            "correct_output": "olleh",
            "hint": "You need to iterate in reverse order."
        },

        # ── HARD: Multiple Bugs + Unit Tests ────────────────────────

        {
            "id": "hard_001",
            "difficulty": "hard",
            "description": (
                "Fix the BankAccount class. "
                "It has 3 bugs. All unit tests must pass."
            ),
            "buggy_code": (
                "class BankAccount:\n"
                "    def __init__(self, balance):\n"
                "        self.balance = balance\n\n"
                "    def deposit(self, amount):\n"
                "        if amount > 0:\n"
                "            self.balance += amount\n\n"
                "    def withdraw(self, amount):\n"
                "        if amount > self.balance:\n"
                "            return 'Insufficient funds'\n"
                "        self.balance -= amount\n\n"
                "    def get_balance(self):\n"
                "        return self.balance\n\n"
                "acc = BankAccount(100)\n"
                "acc.deposit(50)\n"
                "acc.withdraw(200)\n"
                "print(acc.get_balance())"
            ),
            "error_message": (
                "Test 1 FAILED: deposit(-50) should not change balance\n"
                "Test 2 FAILED: withdraw(200) on balance 150 should return "
                "'Insufficient funds' and not change balance\n"
                "Test 3 FAILED: get_balance() after deposit(50) on 100 "
                "should return 150"
            ),
            "correct_output": "150",
            "hint": "Check deposit validation, withdraw condition, and return values."
        },
        {
            "id": "hard_002",
            "difficulty": "hard",
            "description": (
                "Fix the binary search function. "
                "Must pass all 3 unit tests."
            ),
            "buggy_code": (
                "def binary_search(arr, target):\n"
                "    left, right = 0, len(arr)\n"
                "    while left < right:\n"
                "        mid = (left + right) // 2\n"
                "        if arr[mid] == target:\n"
                "            return mid\n"
                "        elif arr[mid] < target:\n"
                "            left = mid\n"
                "        else:\n"
                "            right = mid\n"
                "    return -1\n"
                "print(binary_search([1,3,5,7,9], 5))"
            ),
            "error_message": (
                "Test 1 FAILED: binary_search([1,3,5,7,9], 5) loops forever\n"
                "Test 2 FAILED: binary_search([1,3,5,7,9], 1) loops forever\n"
                "Test 3 FAILED: binary_search([1,3,5,7,9], 10) should return -1"
            ),
            "correct_output": "2",
            "hint": "Check the initial right boundary and how left/right are updated."
        },
        {
            "id": "hard_003",
            "difficulty": "hard",
            "description": (
                "Fix the function that flattens a nested list. "
                "Must pass all 3 unit tests."
            ),
            "buggy_code": (
                "def flatten(lst):\n"
                "    result = []\n"
                "    for item in lst:\n"
                "        if type(item) == list:\n"
                "            result += flatten(item)\n"
                "        else:\n"
                "            result.append(item)\n"
                "    return result\n\n"
                "print(flatten([1, [2, [3, 4]], 5]))"
            ),
            "error_message": (
                "Test 1 FAILED: flatten([1,[2,[3,4]],5]) should return "
                "[1,2,3,4,5]\n"
                "Test 2 FAILED: does not handle tuples\n"
                "Test 3 FAILED: does not handle empty nested lists"
            ),
            "correct_output": "[1, 2, 3, 4, 5]",
            "hint": "Use isinstance() instead of type() to handle subclasses."
        },
    ]


def get_challenge_by_id(challenge_id: str) -> Dict[str, Any]:
    for c in get_challenges():
        if c["id"] == challenge_id:
            return c
    raise ValueError(f"Challenge '{challenge_id}' not found.")


def get_by_difficulty(difficulty: str) -> List[Dict[str, Any]]:
    return [c for c in get_challenges() if c["difficulty"] == difficulty]