Repository navigation
Expand file tree
/
Copy pathbug_bank.py
More file actions
608 lines (598 loc) · 19.4 KB
/
Copy pathbug_bank.py
File metadata and controls
608 lines (598 loc) · 19.4 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
"""
Bug Bank - Collection of buggy Python code problems.
Contains 30+ problems across 8 bug categories for BugLab.
"""
PROBLEMS = [
# ============================================================
# CATEGORY 1: Logic Errors (wrong operators)
# ============================================================
{
"id": "logic_001",
"difficulty": "easy",
"category": "logic_error",
"description": "Function should return the maximum of two numbers",
"buggy_code": """def find_max(a, b):
if a < b:
return a
return b""",
"fixed_code": """def find_max(a, b):
if a > b:
return a
return b""",
"test_cases": [
{"input": [5, 3], "expected": 5},
{"input": [2, 8], "expected": 8},
{"input": [7, 7], "expected": 7},
]
},
{
"id": "logic_002",
"difficulty": "easy",
"category": "logic_error",
"description": "Function should check if a number is even",
"buggy_code": """def is_even(n):
return n % 2 == 1""",
"fixed_code": """def is_even(n):
return n % 2 == 0""",
"test_cases": [
{"input": [4], "expected": True},
{"input": [7], "expected": False},
{"input": [0], "expected": True},
]
},
{
"id": "logic_003",
"difficulty": "medium",
"category": "logic_error",
"description": "Function should return True if score is passing (>= 60)",
"buggy_code": """def is_passing(score):
return score > 60""",
"fixed_code": """def is_passing(score):
return score >= 60""",
"test_cases": [
{"input": [70], "expected": True},
{"input": [60], "expected": True},
{"input": [59], "expected": False},
]
},
{
"id": "logic_004",
"difficulty": "easy",
"category": "logic_error",
"description": "Function should add two numbers",
"buggy_code": """def add(a, b):
return a - b""",
"fixed_code": """def add(a, b):
return a + b""",
"test_cases": [
{"input": [2, 3], "expected": 5},
{"input": [10, -5], "expected": 5},
{"input": [0, 0], "expected": 0},
]
},
# ============================================================
# CATEGORY 2: Off-by-One Errors
# ============================================================
{
"id": "off_by_one_001",
"difficulty": "medium",
"category": "off_by_one",
"description": "Function should return first N natural numbers (1 to N inclusive)",
"buggy_code": """def first_n_numbers(n):
return list(range(1, n))""",
"fixed_code": """def first_n_numbers(n):
return list(range(1, n + 1))""",
"test_cases": [
{"input": [5], "expected": [1, 2, 3, 4, 5]},
{"input": [1], "expected": [1]},
{"input": [3], "expected": [1, 2, 3]},
]
},
{
"id": "off_by_one_002",
"difficulty": "medium",
"category": "off_by_one",
"description": "Function should return the last element of a list",
"buggy_code": """def get_last(lst):
return lst[len(lst)]""",
"fixed_code": """def get_last(lst):
return lst[len(lst) - 1]""",
"test_cases": [
{"input": [[1, 2, 3]], "expected": 3},
{"input": [["a", "b"]], "expected": "b"},
{"input": [[42]], "expected": 42},
]
},
{
"id": "off_by_one_003",
"difficulty": "easy",
"category": "off_by_one",
"description": "Function should count from 0 to n (inclusive)",
"buggy_code": """def count_to_n(n):
return list(range(n))""",
"fixed_code": """def count_to_n(n):
return list(range(n + 1))""",
"test_cases": [
{"input": [3], "expected": [0, 1, 2, 3]},
{"input": [0], "expected": [0]},
{"input": [5], "expected": [0, 1, 2, 3, 4, 5]},
]
},
{
"id": "off_by_one_004",
"difficulty": "medium",
"category": "off_by_one",
"description": "Function should sum all numbers from 1 to N inclusive",
"buggy_code": """def sum_to_n(n):
total = 0
for i in range(1, n):
total += i
return total""",
"fixed_code": """def sum_to_n(n):
total = 0
for i in range(1, n + 1):
total += i
return total""",
"test_cases": [
{"input": [5], "expected": 15},
{"input": [1], "expected": 1},
{"input": [10], "expected": 55},
]
},
# ============================================================
# CATEGORY 3: Wrong Return Value
# ============================================================
{
"id": "return_001",
"difficulty": "easy",
"category": "wrong_return",
"description": "Function should return the square of a number",
"buggy_code": """def square(n):
result = n * n
return n""",
"fixed_code": """def square(n):
result = n * n
return result""",
"test_cases": [
{"input": [4], "expected": 16},
{"input": [0], "expected": 0},
{"input": [7], "expected": 49},
]
},
{
"id": "return_002",
"difficulty": "medium",
"category": "wrong_return",
"description": "Function should return the length of a string",
"buggy_code": """def string_length(s):
count = 0
for char in s:
count += 1
return s""",
"fixed_code": """def string_length(s):
return len(s)""",
"test_cases": [
{"input": ["hello"], "expected": 5},
{"input": [""], "expected": 0},
{"input": ["test"], "expected": 4},
]
},
{
"id": "return_003",
"difficulty": "easy",
"category": "wrong_return",
"description": "Function should return True if list is empty",
"buggy_code": """def is_empty(lst):
if len(lst) == 0:
return False
return True""",
"fixed_code": """def is_empty(lst):
if len(lst) == 0:
return True
return False""",
"test_cases": [
{"input": [[]], "expected": True},
{"input": [[1, 2]], "expected": False},
{"input": [[0]], "expected": False},
]
},
{
"id": "return_004",
"difficulty": "medium",
"category": "wrong_return",
"description": "Function should return the minimum value in a list",
"buggy_code": """def find_min(numbers):
minimum = numbers[0]
for num in numbers:
if num < minimum:
minimum = num
return numbers[0]""",
"fixed_code": """def find_min(numbers):
minimum = numbers[0]
for num in numbers:
if num < minimum:
minimum = num
return minimum""",
"test_cases": [
{"input": [[5, 2, 8, 1]], "expected": 1},
{"input": [[10, 20, 30]], "expected": 10},
{"input": [[-5, -2, -10]], "expected": -10},
]
},
# ============================================================
# CATEGORY 4: Missing Edge Cases
# ============================================================
{
"id": "edge_001",
"difficulty": "medium",
"category": "missing_edge_case",
"description": "Function should return the first element, or None if list is empty",
"buggy_code": """def first_or_none(lst):
return lst[0]""",
"fixed_code": """def first_or_none(lst):
if len(lst) == 0:
return None
return lst[0]""",
"test_cases": [
{"input": [[1, 2, 3]], "expected": 1},
{"input": [[]], "expected": None},
{"input": [["a"]], "expected": "a"},
]
},
{
"id": "edge_002",
"difficulty": "medium",
"category": "missing_edge_case",
"description": "Function should safely divide two numbers, return None if divisor is zero",
"buggy_code": """def safe_divide(a, b):
return a / b""",
"fixed_code": """def safe_divide(a, b):
if b == 0:
return None
return a / b""",
"test_cases": [
{"input": [10, 2], "expected": 5.0},
{"input": [10, 0], "expected": None},
{"input": [7, 2], "expected": 3.5},
]
},
{
"id": "edge_003",
"difficulty": "hard",
"category": "missing_edge_case",
"description": "Function should return the average of a list, or 0 if empty",
"buggy_code": """def average(numbers):
return sum(numbers) / len(numbers)""",
"fixed_code": """def average(numbers):
if len(numbers) == 0:
return 0
return sum(numbers) / len(numbers)""",
"test_cases": [
{"input": [[1, 2, 3]], "expected": 2.0},
{"input": [[]], "expected": 0},
{"input": [[10]], "expected": 10.0},
]
},
{
"id": "edge_004",
"difficulty": "medium",
"category": "missing_edge_case",
"description": "Function should find index of element, return -1 if not found",
"buggy_code": """def find_index(lst, target):
for i in range(len(lst)):
if lst[i] == target:
return i""",
"fixed_code": """def find_index(lst, target):
for i in range(len(lst)):
if lst[i] == target:
return i
return -1""",
"test_cases": [
{"input": [[1, 2, 3], 2], "expected": 1},
{"input": [[1, 2, 3], 5], "expected": -1},
{"input": [[], 1], "expected": -1},
]
},
# ============================================================
# CATEGORY 5: Type Errors
# ============================================================
{
"id": "type_001",
"difficulty": "easy",
"category": "type_error",
"description": "Function should concatenate a string and a number",
"buggy_code": """def concat_str_num(s, n):
return s + n""",
"fixed_code": """def concat_str_num(s, n):
return s + str(n)""",
"test_cases": [
{"input": ["Score: ", 95], "expected": "Score: 95"},
{"input": ["Count: ", 0], "expected": "Count: 0"},
{"input": ["Value: ", 42], "expected": "Value: 42"},
]
},
{
"id": "type_002",
"difficulty": "medium",
"category": "type_error",
"description": "Function should repeat a string N times",
"buggy_code": """def repeat_string(s, n):
return s * str(n)""",
"fixed_code": """def repeat_string(s, n):
return s * n""",
"test_cases": [
{"input": ["ab", 3], "expected": "ababab"},
{"input": ["x", 1], "expected": "x"},
{"input": ["hi", 0], "expected": ""},
]
},
{
"id": "type_003",
"difficulty": "easy",
"category": "type_error",
"description": "Function should convert string input to integer and add 10",
"buggy_code": """def add_ten(s):
return s + 10""",
"fixed_code": """def add_ten(s):
return int(s) + 10""",
"test_cases": [
{"input": ["5"], "expected": 15},
{"input": ["0"], "expected": 10},
{"input": ["25"], "expected": 35},
]
},
{
"id": "type_004",
"difficulty": "medium",
"category": "type_error",
"description": "Function should sum string numbers in a list",
"buggy_code": """def sum_string_numbers(str_nums):
total = 0
for s in str_nums:
total += s
return total""",
"fixed_code": """def sum_string_numbers(str_nums):
total = 0
for s in str_nums:
total += int(s)
return total""",
"test_cases": [
{"input": [["1", "2", "3"]], "expected": 6},
{"input": [["10", "20"]], "expected": 30},
{"input": [["5"]], "expected": 5},
]
},
# ============================================================
# CATEGORY 6: Recursion Errors (missing base case)
# ============================================================
{
"id": "recursion_001",
"difficulty": "hard",
"category": "recursion_error",
"description": "Function should calculate factorial using recursion",
"buggy_code": """def factorial(n):
return n * factorial(n - 1)""",
"fixed_code": """def factorial(n):
if n <= 1:
return 1
return n * factorial(n - 1)""",
"test_cases": [
{"input": [5], "expected": 120},
{"input": [0], "expected": 1},
{"input": [3], "expected": 6},
]
},
{
"id": "recursion_002",
"difficulty": "hard",
"category": "recursion_error",
"description": "Function should sum all numbers from n down to 0",
"buggy_code": """def sum_down(n):
return n + sum_down(n - 1)""",
"fixed_code": """def sum_down(n):
if n <= 0:
return 0
return n + sum_down(n - 1)""",
"test_cases": [
{"input": [5], "expected": 15},
{"input": [0], "expected": 0},
{"input": [3], "expected": 6},
]
},
{
"id": "recursion_003",
"difficulty": "hard",
"category": "recursion_error",
"description": "Function should count down from N to 1",
"buggy_code": """def countdown(n):
result = [n]
result.extend(countdown(n - 1))
return result""",
"fixed_code": """def countdown(n):
if n <= 0:
return []
result = [n]
result.extend(countdown(n - 1))
return result""",
"test_cases": [
{"input": [3], "expected": [3, 2, 1]},
{"input": [1], "expected": [1]},
{"input": [5], "expected": [5, 4, 3, 2, 1]},
]
},
# ============================================================
# CATEGORY 7: Loop Errors
# ============================================================
{
"id": "loop_001",
"difficulty": "medium",
"category": "loop_error",
"description": "Function should count occurrences of target in list",
"buggy_code": """def count_occurrences(lst, target):
count = 0
for item in lst:
if item == target:
count += 1
return count
count = 0""",
"fixed_code": """def count_occurrences(lst, target):
count = 0
for item in lst:
if item == target:
count += 1
return count""",
"test_cases": [
{"input": [[1, 2, 3, 2, 2], 2], "expected": 3},
{"input": [[1, 1, 1], 1], "expected": 3},
{"input": [[1, 2, 3], 5], "expected": 0},
]
},
{
"id": "loop_002",
"difficulty": "easy",
"category": "loop_error",
"description": "Function should reverse a list",
"buggy_code": """def reverse_list(lst):
result = []
for i in range(len(lst)):
result.append(lst[i])
return result""",
"fixed_code": """def reverse_list(lst):
result = []
for i in range(len(lst) - 1, -1, -1):
result.append(lst[i])
return result""",
"test_cases": [
{"input": [[1, 2, 3]], "expected": [3, 2, 1]},
{"input": [["a", "b"]], "expected": ["b", "a"]},
{"input": [[5]], "expected": [5]},
]
},
{
"id": "loop_003",
"difficulty": "medium",
"category": "loop_error",
"description": "Function should collect all even numbers from a list",
"buggy_code": """def get_evens(numbers):
evens = []
for num in numbers:
if num % 2 == 0:
evens.append(num)
break
return evens""",
"fixed_code": """def get_evens(numbers):
evens = []
for num in numbers:
if num % 2 == 0:
evens.append(num)
return evens""",
"test_cases": [
{"input": [[1, 2, 3, 4, 5]], "expected": [2, 4]},
{"input": [[2, 4, 6]], "expected": [2, 4, 6]},
{"input": [[1, 3, 5]], "expected": []},
]
},
{
"id": "loop_004",
"difficulty": "hard",
"category": "loop_error",
"description": "Function should build a multiplication table",
"buggy_code": """def mult_table(n):
table = []
for i in range(1, n + 1):
row = []
for j in range(1, n + 1):
row.append(i * i)
table.append(row)
return table""",
"fixed_code": """def mult_table(n):
table = []
for i in range(1, n + 1):
row = []
for j in range(1, n + 1):
row.append(i * j)
table.append(row)
return table""",
"test_cases": [
{"input": [2], "expected": [[1, 2], [2, 4]]},
{"input": [1], "expected": [[1]]},
{"input": [3], "expected": [[1, 2, 3], [2, 4, 6], [3, 6, 9]]},
]
},
# ============================================================
# CATEGORY 8: Variable Shadowing
# ============================================================
{
"id": "shadow_001",
"difficulty": "medium",
"category": "variable_shadowing",
"description": "Function should sum a list and return the total",
"buggy_code": """def sum_list(numbers):
sum = 0
for num in numbers:
sum = num
return sum""",
"fixed_code": """def sum_list(numbers):
sum = 0
for num in numbers:
sum += num
return sum""",
"test_cases": [
{"input": [[1, 2, 3]], "expected": 6},
{"input": [[10, 20]], "expected": 30},
{"input": [[5]], "expected": 5},
]
},
{
"id": "shadow_002",
"difficulty": "hard",
"category": "variable_shadowing",
"description": "Function should collect unique values from a list",
"buggy_code": """def get_unique(lst):
unique = []
for item in lst:
unique = item
if item not in unique:
unique.append(item)
return unique""",
"fixed_code": """def get_unique(lst):
unique = []
for item in lst:
if item not in unique:
unique.append(item)
return unique""",
"test_cases": [
{"input": [[1, 2, 2, 3]], "expected": [1, 2, 3]},
{"input": [[5, 5, 5]], "expected": [5]},
{"input": [[1, 2, 3]], "expected": [1, 2, 3]},
]
},
{
"id": "shadow_003",
"difficulty": "medium",
"category": "variable_shadowing",
"description": "Function should find the product of all numbers in a list",
"buggy_code": """def product(numbers):
result = 1
for num in numbers:
result = num
return result""",
"fixed_code": """def product(numbers):
result = 1
for num in numbers:
result *= num
return result""",
"test_cases": [
{"input": [[2, 3, 4]], "expected": 24},
{"input": [[5, 2]], "expected": 10},
{"input": [[7]], "expected": 7},
]
},
]
# Verify we have 30+ problems
assert len(PROBLEMS) >= 30, f"Need at least 30 problems, have {len(PROBLEMS)}"
# Verify all required fields present
required_fields = {"id", "difficulty", "category", "description", "buggy_code", "fixed_code", "test_cases"}
for problem in PROBLEMS:
assert required_fields.issubset(problem.keys()), f"Problem {problem.get('id')} missing required fields"
assert len(problem["test_cases"]) >= 3, f"Problem {problem.get('id')} needs at least 3 test cases"