-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_levels.py
More file actions
96 lines (91 loc) · 3.02 KB
/
generate_levels.py
File metadata and controls
96 lines (91 loc) · 3.02 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
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
import json
import random
# Basic level bank (you can expand this list)
level_templates = [
{
"title": "Even or Odd",
"instruction": "Return 'even' if the number is even, else 'odd'.",
"function_name": "even_or_odd",
"inputs": [2, 3, 10, 11],
"outputs": ["even", "odd", "even", "odd"],
"hint": "Use % to check divisibility by 2."
},
{
"title": "Double the Number",
"instruction": "Return the input number multiplied by 2.",
"function_name": "double",
"inputs": [1, 5, -3],
"outputs": [2, 10, -6],
"hint": "Use the * operator."
},
{
"title": "Sum of Two Numbers",
"instruction": "Return the sum of two numbers.",
"function_name": "sum_two",
"inputs": ["2, 3", "7, 5"],
"outputs": [5, 12],
"hint": "Split the input and add the numbers."
},
{
"title": "String Reverser",
"instruction": "Return the reversed string.",
"function_name": "reverse_string",
"inputs": ["hello", "world"],
"outputs": ["olleh", "dlrow"],
"hint": "Use slicing like s[::-1]"
},
{
"title": "Count Vowels",
"instruction": "Return number of vowels in the string.",
"function_name": "count_vowels",
"inputs": ["apple", "xyz", "education"],
"outputs": [2, 0, 5],
"hint": "Loop through the string and count aeiou."
},
{
"title": "Is Positive?",
"instruction": "Return True if the number is positive.",
"function_name": "is_positive",
"inputs": [5, -2, 0],
"outputs": [True, False, False],
"hint": "Check if number > 0"
},
{
"title": "Square a Number",
"instruction": "Return the square of a number.",
"function_name": "square",
"inputs": [2, -4, 10],
"outputs": [4, 16, 100],
"hint": "Use number * number"
},
{
"title": "Length of String",
"instruction": "Return the length of a string.",
"function_name": "string_length",
"inputs": ["hi", "hello", "wow"],
"outputs": [2, 5, 3],
"hint": "Use len() function"
}
]
def generate_levels(num_levels=100):
levels = {}
for i in range(1, num_levels + 1):
template = random.choice(level_templates)
idx = random.randint(0, len(template["inputs"]) - 1)
input_val = template["inputs"][idx]
output_val = template["outputs"][idx]
levels[str(i)] = {
"title": template["title"],
"instruction": template["instruction"],
"function_name": template["function_name"],
"test_input": input_val,
"expected_output": output_val,
"hint": template["hint"]
}
return levels
# Save to levels.json
if __name__ == "__main__":
generated = generate_levels(100)
with open("levels.json", "w", encoding="utf-8") as f:
json.dump(generated, f, indent=2)
print("✅ Generated 100 levels and saved to levels.json")