Spaces:
Sleeping
Sleeping
File size: 3,506 Bytes
637f42c | 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 | """
EASY TASK: Fix the off-by-one bug in a list-processing utility.
The repo has:
- utils/list_ops.py β contains a buggy `chunk_list` function
- tests/test_list_ops.py β 3 tests (1 currently failing)
The agent must identify and fix the bug so all tests pass,
with clean lint, and commit with a meaningful message.
"""
from __future__ import annotations
TASK_ID = "easy_bugfix_chunk_list"
DIFFICULTY = "easy"
MAX_STEPS = 20
DESCRIPTION = """
## Task: Fix the `chunk_list` Bug
The function `chunk_list(lst, n)` in `utils/list_ops.py` is supposed to split a
list into consecutive chunks of size `n`.
**Example:**
chunk_list([1,2,3,4,5], 2) β [[1,2],[3,4],[5]]
**Problem:** There is an off-by-one error causing the last chunk to be dropped.
**Your job:**
1. Read the source and tests
2. Identify the exact bug
3. Fix `utils/list_ops.py` (do NOT modify tests)
4. Ensure all 3 tests pass
5. Run lint and fix any issues
6. Write a review of what you found
7. Commit with a conventional-commit message
"""
# ββ Initial repo snapshot βββββββββββββββββββββββββββββββββββββββββββββββββββββ
INITIAL_FILES: dict[str, str] = {
"utils/__init__.py": "",
"utils/list_ops.py": """\
\"\"\"List utility operations.\"\"\"
from typing import Any, List
def chunk_list(lst: List[Any], n: int) -> List[List[Any]]:
\"\"\"Split *lst* into consecutive chunks of size *n*.
The final chunk may be smaller than *n* if len(lst) is not divisible.
Example:
>>> chunk_list([1, 2, 3, 4, 5], 2)
[[1, 2], [3, 4], [5]]
\"\"\"
if n <= 0:
raise ValueError("Chunk size must be positive")
result = []
# BUG: range stops one short β should be range(0, len(lst), n)
for i in range(0, len(lst) - 1, n):
result.append(lst[i : i + n])
return result
def flatten(lst: List[List[Any]]) -> List[Any]:
\"\"\"Flatten a list of lists by one level.\"\"\"
return [item for sublist in lst for item in sublist]
""",
"tests/__init__.py": "",
"tests/test_list_ops.py": """\
\"\"\"Tests for list_ops utilities.\"\"\"
import pytest
from utils.list_ops import chunk_list, flatten
class TestChunkList:
def test_even_split(self):
assert chunk_list([1, 2, 3, 4], 2) == [[1, 2], [3, 4]]
def test_odd_split(self):
# Last chunk is smaller β this currently FAILS due to the bug
assert chunk_list([1, 2, 3, 4, 5], 2) == [[1, 2], [3, 4], [5]]
def test_chunk_larger_than_list(self):
assert chunk_list([1, 2], 5) == [[1, 2]]
def test_empty_list(self):
assert chunk_list([], 3) == []
def test_invalid_chunk_size(self):
with pytest.raises(ValueError):
chunk_list([1, 2], 0)
class TestFlatten:
def test_basic(self):
assert flatten([[1, 2], [3, 4]]) == [1, 2, 3, 4]
def test_empty(self):
assert flatten([]) == []
""",
"pyproject.toml": """\
[tool.ruff]
line-length = 88
select = ["E", "F", "W"]
ignore = []
""",
"README.md": "# BugFix Challenge\n\nFix the `chunk_list` bug.\n",
}
# ββ Expected outputs (for grader) ββββββββββββββββββββββββββββββββββββββββββββ
EXPECTED_FIXED_SNIPPET = "range(0, len(lst), n)"
REQUIRED_KEYWORDS_IN_REVIEW = [
"off-by-one",
"range",
"chunk",
]
PASSING_TESTS = 7 # all tests in the file
|