| """`file_splitting` task generator.""" |
| import json |
| import random |
| import shutil |
| from collections import defaultdict |
| from pathlib import Path |
|
|
| from ..base import ( |
| Generator, _VERIFY_HEADER, _render_verify, pad_to, _write, _para, |
| _AUTHORS, _SONG_TITLES, _STUDENTS, _WORDS, |
| ) |
|
|
|
|
| class FileSplitting(Generator): |
| KEY = "file_splitting" |
| CATEGORY_NAME = "File Splitting" |
| DIFFICULTY = "L2" |
| TAGS = ["file content", "split"] |
|
|
| SRC = "combined.txt" |
| DELIM = "-----" |
| OUT_DIR = "parts" |
|
|
| def build(self, env_dir, llm, rng): |
| k = rng.randint(3, 5) |
| sections = [] |
| for _ in range(k): |
| sections.append("\n".join(_para(rng, rng.randint(2, 4)))) |
| content = ("\n" + self.DELIM + "\n").join(sections) |
| _write(env_dir / self.SRC, content + "\n") |
| return {"src": self.SRC, "delim": self.DELIM, "out_dir": self.OUT_DIR, "k": k} |
|
|
| def description(self, spec): |
| return ( |
| "Please use FileSystem tools to finish the following task:\n\n" |
| "### Task: Split a combined file into parts\n\n" |
| f"The file `{self.SRC}` contains several sections separated by lines that " |
| f"consist of exactly `{self.DELIM}`.\n\n" |
| f"1. Create a folder named `{self.OUT_DIR}/` in the test directory root.\n" |
| f"2. Write each section, in order, to `{self.OUT_DIR}/part_1.txt`, " |
| f"`{self.OUT_DIR}/part_2.txt`, … (1-based).\n" |
| "3. Each part file contains only that section's text (no delimiter " |
| "lines). Do not modify the original file." |
| ) |
|
|
| def verify_src(self, spec): |
| body = ''' |
| C = json.loads(__CONSTS__) |
| |
| |
| def main(): |
| t = get_test_dir() |
| src = t / C["src"] |
| if not src.is_file(): |
| fail(f"source file missing: {C['src']}") |
| sections = src.read_text(encoding="utf-8").split("\\n" + C["delim"] + "\\n") |
| pdir = t / C["out_dir"] |
| if not pdir.is_dir(): |
| fail(f"missing directory: {C['out_dir']}") |
| for i, sec in enumerate(sections, 1): |
| pf = pdir / f"part_{i}.txt" |
| if not pf.is_file(): |
| fail(f"missing part file: part_{i}.txt") |
| if pf.read_text(encoding="utf-8").strip() != sec.strip(): |
| fail(f"part_{i}.txt content does not match section {i}") |
| extra = [p.name for p in pdir.iterdir() if p.is_file() and p.name not in |
| {f"part_{i}.txt" for i in range(1, len(sections) + 1)}] |
| if extra: |
| fail(f"unexpected files in {C['out_dir']}: {extra}") |
| ok(f"file correctly split into {len(sections)} parts") |
| print("\\U0001f389 All checks passed!") |
| sys.exit(0) |
| |
| |
| if __name__ == "__main__": |
| main() |
| ''' |
| return _render_verify(body, {"src": spec["src"], "delim": spec["delim"], "out_dir": spec["out_dir"]}) |
|
|
| def solve(self, work_dir, spec): |
| src = work_dir / spec["src"] |
| sections = src.read_text(encoding="utf-8").split("\n" + spec["delim"] + "\n") |
| pdir = work_dir / spec["out_dir"] |
| pdir.mkdir(exist_ok=True) |
| for i, sec in enumerate(sections, 1): |
| _write(pdir / f"part_{i}.txt", sec.strip() + "\n") |
|
|