Spaces:
Sleeping
Sleeping
| """ | |
| 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 | |
| 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()) | |