-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_problem_json.py
executable file
·146 lines (120 loc) · 3.75 KB
/
make_problem_json.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
from __future__ import annotations # Pre 3.9
import argparse
import os
import os.path
import abbrs
title, description, in_count = '', [ ], -1
def get_script_information(filename: str) -> None:
global title, description, in_count
lines = abbrs.read_file(filename).split('\n')
assert lines[0][:2] == '# '
title = lines[0][2:]
assert lines[1] == "'''"
description = [ ]
in_count = -1
for i in lines[2:]:
if i == "'''":
break
else:
description.append(i)
if in_count == -1:
x = parse_digit(i)
if x != -1:
in_count = x
else:
assert False
def parse_digit(x: str) -> int:
x = [ i if i.isdigit() else ' ' for i in x ]
return int(''.join(x).split()[0])
def get_case_input_dat_count(filename: str) -> int:
'''only for external callers'''
get_script_information(filename)
return in_count
def str2unicode(s: str) -> str:
s = ascii(s)
s = list(s)
s.pop()
s.pop(0)
return ''.join(s)
def check_and_filter_in_out_files_folder(files: list[str]) -> list[int]:
assert len(files) % 2 == 0 # in pairs
out = [ ]
# check files format
for i in files:
parts = i.split('.')
assert len(parts) == 2
no, ext = parts
assert no.isdigit()
assert ext in [ 'in', 'out' ]
if ext == 'in':
assert f'{no}.out' in files
out.append(no)
else:
assert ext == 'out' # double-check
assert f'{no}.in' in files
return out
def make_in_out_files_configuration(files: list[str], TEMPLATE: str) -> str:
return ',\n'.join([ TEMPLATE.format(i) for i in
check_and_filter_in_out_files_folder(files) ])
def make_test_case_conf(testcase_home: str) -> str:
PROBLEM_JSON = 'problem.json'
files = os.listdir(testcase_home)
# drop problem.json if any
assert files.count(PROBLEM_JSON) in [ 0, 1 ]
if PROBLEM_JSON in files:
files.remove(PROBLEM_JSON)
# generate configuration
return make_in_out_files_configuration(files, '''
{{
"score": 100,
"input_name": "{0}.in",
"output_name": "{0}.out"
}}''')
def make_sample(sample_home: str) -> str:
return make_in_out_files_configuration(os.listdir(sample_home), '''
{{
"input": "{0}",
"output": "{0}"
}}
''')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Generate problem.json.')
parser.add_argument('filename')
parser.add_argument('testcase_home')
parser.add_argument('sample_home')
args = parser.parse_args()
get_script_information(args.filename)
S1 = str2unicode('详见样例')
print('''{
"display_id": "%s",
"title": "%s",
"description": {
"format": "html",
"value": "<pre>%s</pre>"
},
"tags": [ "PP" ],
"input_description": {
"format": "html",
"value": "<p>%s</p>"
},
"output_description": {
"format": "html",
"value": "<p>%s</p>"
},
"hint": { "format": "html", "value": "" },
"time_limit": 1000,
"memory_limit": 256,
"template": {},
"spj": null,
"rule_type": "ACM",
"source": "Python%s http://222.201.101.8",
"answers": [],
"test_case_score": [ %s ],
"sample": [ %s ]
}''' % ('PP-' + os.path.basename(args.filename).split('.')[0],
str2unicode(title),
str2unicode('\n'.join(description)),
S1, S1, str2unicode('刷题系统'),
make_test_case_conf(args.testcase_home),
make_sample(args.sample_home)))