Spaces:
Sleeping
Sleeping
File size: 10,048 Bytes
71a1c53 | 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 | """
Problem Bank
------------
Debugging challenges across three difficulty tiers.
Each problem is a dict with:
- name: short identifier
- description: what the function should do (natural language)
- buggy_code: the broken implementation
- function_name: entry-point the tests call
- tests: list of (args_tuple, expected_return_value)
"""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Any, List, Tuple
@dataclass
class Problem:
name: str
description: str
buggy_code: str
function_name: str
tests: List[Tuple[tuple, Any]] = field(default_factory=list)
# ---------------------------------------------------------------------------
# EASY -- Syntax errors
# ---------------------------------------------------------------------------
EASY_PROBLEMS: List[Problem] = [
Problem(
name="missing_colon",
description=(
"Write a function `is_even(n)` that returns True when n is even "
"and False otherwise."
),
buggy_code=(
"def is_even(n)\n"
" if n % 2 == 0:\n"
" return True\n"
" return False\n"
),
function_name="is_even",
tests=[
((0,), True),
((1,), False),
((2,), True),
((-3,), False),
((100,), True),
],
),
Problem(
name="unmatched_paren",
description=(
"Write a function `farewell(name)` that returns the string "
"'Goodbye, <name>' (e.g. farewell('Alice') -> 'Goodbye, Alice')."
),
buggy_code=(
"def farewell(name):\n"
' return "Goodbye, " + str(name\n'
),
function_name="farewell",
tests=[
(("Alice",), "Goodbye, Alice"),
(("Bob",), "Goodbye, Bob"),
(("",), "Goodbye, "),
(("World",), "Goodbye, World"),
],
),
Problem(
name="bad_indentation",
description=(
"Write a function `sum_list(lst)` that returns the sum of all "
"elements in a list of numbers."
),
buggy_code=(
"def sum_list(lst):\n"
" total = 0\n"
" for item in lst:\n"
" total += item\n"
" return total\n"
),
function_name="sum_list",
tests=[
(([],), 0),
(([1, 2, 3],), 6),
(([10, -5, 3],), 8),
(([-1, -2, -3],), -6),
(([100],), 100),
],
),
]
# ---------------------------------------------------------------------------
# MEDIUM -- Logic errors
# ---------------------------------------------------------------------------
MEDIUM_PROBLEMS: List[Problem] = [
Problem(
name="off_by_one_max",
description=(
"Write a function `find_max(lst)` that returns the largest "
"element in a non-empty list. Return None for an empty list."
),
buggy_code=(
"def find_max(lst):\n"
" if not lst:\n"
" return None\n"
" max_val = lst[0]\n"
" for i in range(1, len(lst) - 1):\n"
" if lst[i] > max_val:\n"
" max_val = lst[i]\n"
" return max_val\n"
),
function_name="find_max",
tests=[
(([],), None),
(([5],), 5),
(([1, 2, 3],), 3),
(([3, 1, 2],), 3),
(([1, 3, 2],), 3),
(([-5, -1, -10],), -1),
],
),
Problem(
name="wrong_operator_palindrome",
description=(
"Write a function `is_palindrome(s)` that returns True if the "
"string s reads the same forwards and backwards (case-insensitive)."
),
buggy_code=(
"def is_palindrome(s):\n"
" s = s.lower()\n"
" left, right = 0, len(s) - 1\n"
" while left > right:\n"
" if s[left] != s[right]:\n"
" return False\n"
" left += 1\n"
" right -= 1\n"
" return True\n"
),
function_name="is_palindrome",
tests=[
(("",), True),
(("a",), True),
(("Racecar",), True),
(("hello",), False),
(("abba",), True),
(("abcd",), False),
],
),
Problem(
name="missing_edge_case_flatten",
description=(
"Write a function `flatten(nested)` that takes a list which may "
"contain integers or sublists of integers (one level deep) and "
"returns a flat list of all the integers."
),
buggy_code=(
"def flatten(nested):\n"
" result = []\n"
" for item in nested:\n"
" if isinstance(item, list):\n"
" for sub in item:\n"
" result.append(sub)\n"
" return result\n"
),
function_name="flatten",
tests=[
(([],), []),
(([1, 2, 3],), [1, 2, 3]),
(([[1, 2], 3],), [1, 2, 3]),
(([[1], [2, 3]],), [1, 2, 3]),
(([1, [2], 3, [4, 5]],), [1, 2, 3, 4, 5]),
],
),
]
# ---------------------------------------------------------------------------
# HARD -- Algorithmic / subtle bugs
# ---------------------------------------------------------------------------
HARD_PROBLEMS: List[Problem] = [
Problem(
name="binary_search_bounds",
description=(
"Write a function `binary_search(arr, target)` that returns the "
"index of target in a sorted list, or -1 if not found."
),
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 + 1\n"
" else:\n"
" right = mid\n"
" return -1\n"
),
function_name="binary_search",
tests=[
(([], 5), -1),
(([1], 1), 0),
(([1], 2), -1),
(([1, 3, 5, 7, 9], 5), 2),
(([1, 3, 5, 7, 9], 1), 0),
(([1, 3, 5, 7, 9], 9), 4),
(([1, 3, 5, 7, 9], 4), -1),
(([2, 4, 6, 8, 10, 12], 12), 5),
],
),
Problem(
name="merge_sorted_wrong",
description=(
"Write a function `merge_sorted(a, b)` that merges two sorted "
"lists into a single sorted list."
),
buggy_code=(
"def merge_sorted(a, b):\n"
" result = []\n"
" i = j = 0\n"
" while i < len(a) and j < len(b):\n"
" if a[i] <= b[j]:\n"
" result.append(a[i])\n"
" i += 1\n"
" else:\n"
" result.append(b[j])\n"
" j += 1\n"
" return result\n"
),
function_name="merge_sorted",
tests=[
(([], []), []),
(([1], []), [1]),
(([], [2]), [2]),
(([1, 3, 5], [2, 4, 6]), [1, 2, 3, 4, 5, 6]),
(([1, 2], [3, 4, 5]), [1, 2, 3, 4, 5]),
(([1, 1, 1], [1, 1]), [1, 1, 1, 1, 1]),
],
),
Problem(
name="matrix_spiral_bug",
description=(
"Write a function `spiral_order(matrix)` that returns the elements "
"of an MxN matrix in spiral order (clockwise from top-left). "
"Return an empty list for an empty matrix."
),
buggy_code=(
"def spiral_order(matrix):\n"
" if not matrix or not matrix[0]:\n"
" return []\n"
" result = []\n"
" top, bottom = 0, len(matrix) - 1\n"
" left, right = 0, len(matrix[0]) - 1\n"
" while top <= bottom and left <= right:\n"
" for col in range(left, right + 1):\n"
" result.append(matrix[top][col])\n"
" top += 1\n"
" for row in range(top, bottom + 1):\n"
" result.append(matrix[row][right])\n"
" right -= 1\n"
" for col in range(right, left - 1, -1):\n"
" result.append(matrix[bottom][col])\n"
" bottom -= 1\n"
" for row in range(bottom, top - 1, -1):\n"
" result.append(matrix[row][left])\n"
" left += 1\n"
" return result\n"
),
function_name="spiral_order",
tests=[
(([],), []),
(([[1]],), [1]),
(([[1, 2], [3, 4]],), [1, 2, 4, 3]),
(([[1, 2, 3], [4, 5, 6], [7, 8, 9]],), [1, 2, 3, 6, 9, 8, 7, 4, 5]),
(([[1, 2, 3, 4]],), [1, 2, 3, 4]),
(([[1], [2], [3]],), [1, 2, 3]),
],
),
]
# ---------------------------------------------------------------------------
# Lookup
# ---------------------------------------------------------------------------
TASK_PROBLEMS = {
"fix_syntax": EASY_PROBLEMS,
"fix_logic": MEDIUM_PROBLEMS,
"fix_algorithm": HARD_PROBLEMS,
}
ALL_TASK_NAMES = list(TASK_PROBLEMS.keys())
|