# tasks/medium_task.py import random import json import os from datetime import datetime, timedelta from config import INITIAL_STATE_FILE def _make_file(name: str, size_mb: float, days_ago: int) -> dict: accessed = (datetime.now() - timedelta(days=days_ago)).isoformat() created = (datetime.now() - timedelta(days=days_ago + 30)).isoformat() ext = "." + name.rsplit(".", 1)[-1] if "." in name else "" return { "type": "file", "extension": ext, "size_mb": round(size_mb, 2), "created_date": created, "last_accessed": accessed, } def generate_medium_initial_state(seed: int = 7) -> dict: """ Generate a file system with intentional duplicates. Duplicates share the same base name and size but have trailing (1), (2) markers. Medium Task Goal: Find and delete the duplicate copies, keeping only one original. """ random.seed(seed) # Original files + their sizes (MB) originals = [ ("resume.pdf", 1.2), ("invoice_march.pdf", 0.8), ("vacation_beach.jpg", 3.5), ("project_report.docx", 2.1), ("music_mix.mp3", 8.4), ("data_export.csv", 0.5), ("family_photo.jpg", 4.2), ("budget_2023.xlsx", 1.7), ("presentation.pptx", 5.3), ("contract_signed.pdf", 0.9), ] # Non-duplicate files (distractors) unique_files = [ ("notes.txt", 0.1, 20), ("app_backup.zip", 45.0, 90), ("source_code.py", 0.3, 5), ("readme.md", 0.05, 2), ("database_dump.sql", 12.0, 60), ("config.json", 0.02, 1), ("logo_design.svg", 0.4, 15), ("meeting_recording.mp4", 150.0, 30), ("financial_model.xlsx", 3.8, 45), ("legal_brief.pdf", 2.6, 10), ] root = {} duplicates_to_delete = [] for orig_name, size_mb in originals: days = random.randint(30, 200) root[orig_name] = _make_file(orig_name, size_mb, days) # Create 1 or 2 duplicates num_dupes = random.randint(1, 2) ext = "." + orig_name.rsplit(".", 1)[-1] base = orig_name.rsplit(".", 1)[0] for i in range(1, num_dupes + 1): dupe_name = f"{base}({i}){ext}" # Same size ± tiny floating point noise (still rounds to same) dupe_size = round(size_mb + random.uniform(-0.001, 0.001), 3) root[dupe_name] = _make_file(dupe_name, dupe_size, days + random.randint(1, 10)) duplicates_to_delete.append(dupe_name) for fname, size_mb, days in unique_files: root[fname] = _make_file(fname, size_mb, days) state = {"root": root, "_duplicates_to_delete": duplicates_to_delete} return state def get_medium_task_description() -> str: return ( "MEDIUM TASK – Deduplication:\n" "The drive contains many duplicate files. Files with the same base name and " "approximately the same size (e.g., 'resume.pdf' and 'resume(1).pdf') are duplicates.\n" "Your job is to:\n" "1. Scan the drive and identify all duplicate files.\n" "2. Keep ONE copy of each file (the original without the number suffix).\n" "3. Delete all duplicate copies (those with (1), (2), etc. in the name).\n" "4. Do NOT delete or move any non-duplicate files.\n" "Complete the task when all duplicates are removed." ) def get_medium_grading_criteria(initial_state: dict) -> dict: """Return grading criteria for the medium task.""" return { "files_to_delete": initial_state.get("_duplicates_to_delete", []), "files_to_preserve": [ name for name in initial_state["root"] if name not in initial_state.get("_duplicates_to_delete", []) ], } if __name__ == "__main__": state = generate_medium_initial_state() os.makedirs("data", exist_ok=True) with open(INITIAL_STATE_FILE, "w") as f: json.dump(state, f, indent=2) print(f"Generated {len(state['root'])} files ({len(state['_duplicates_to_delete'])} duplicates).") print(f"Saved to {INITIAL_STATE_FILE}")