forked from johnny603/lux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpuzzle_generator.py
More file actions
46 lines (41 loc) · 1.54 KB
/
Copy pathpuzzle_generator.py
File metadata and controls
46 lines (41 loc) · 1.54 KB
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
from __future__ import annotations
from typing import Dict, Iterable, Optional
def _validate_generated_puzzle(puzzle: Dict) -> bool:
if not isinstance(puzzle, dict):
return False
required = {"id", "title", "description", "hint", "validator"}
if not required.issubset(puzzle):
return False
if puzzle.get("validator") == "script" and not puzzle.get("test_script"):
return False
if puzzle.get("validator") not in {"equals", "contains", "script"}:
return False
return True
def generate_puzzle_with_ai(
*,
category: str,
difficulty: str,
topic: str,
existing_ids: Iterable[str],
next_id: Optional[str] = None,
) -> Dict:
category = (category or "Programming").strip() or "Programming"
difficulty = (difficulty or "easy").strip().lower() or "easy"
topic = (topic or "practice").strip() or "practice"
next_id = next_id or "999"
puzzle = {
"id": next_id,
"title": f"{category}: {topic.title()}",
"description": f"Create a short solution for the {topic} challenge.",
"hint": f"Think about the {difficulty} concept for {topic}.",
"validator": "equals",
"flag": "sample-output",
"category": category,
"difficulty": difficulty,
"tags": [category.lower(), topic.lower(), difficulty],
}
if puzzle["id"] in set(existing_ids or []):
puzzle["id"] = str(int(next_id) + 1)
if not _validate_generated_puzzle(puzzle):
raise ValueError("Generated puzzle failed validation")
return puzzle