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