/
leet_challenge.py
299 lines (240 loc) · 8.95 KB
/
leet_challenge.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import monkeypatch
import sys, os, inspect
from commons_static import *
from runcode import run_python_code
import re
challenge_props =cprops= '''
name:
description:
user_code: code given to user
user_code_reference: official answer
test_cases: test cases string given to user
test_cases_submit: used in submission
comment:
'''.split('\n').filter(lambda l:len(l)>5) \
.map(lambda l:l.split(':')).filter(lambda l:len(l)==2) \
.map(lambda l:l[0].strip())
print_info(cprops)
props_exp = r'^(names):.*?$'.replace('names', cprops.join('|'))
def parse_leet_challenge(fn):
cont = readfile(fn, 'r')
lc = {}
c = re.split(props_exp, cont, flags=re.M)
for idx, s in enumerate(c):
if s in cprops:
section = c[idx+1].strip()
res = re.match(r'```.*?\n((?:.|\n)*?)```', section)
if res:
# print(res[1])
section = res[1].strip()
lc[s] = section
return lc
class Printer:
def __init__(self):
self.text = ''
def __call__(self, *args):
self.text += list(args).map(str).join(' ')+'\n'
def b2s(b):return b.decode('utf-8', errors='ignore')
def s2b(s):return s.encode('utf-8', errors='ignore')
assert s2b('hello') == b'hello'
assert b2s(b'hello') == 'hello'
class LeetChallenge:
def __init__(self, fn):
self.fn = fn
if fn:
self.update()
# self.force_online = not get_environ('DEBUG')
self.force_online = True
def update(self):
fn = self.fn
d = parse_leet_challenge(fn)
for k in d:
self.__setattr__(k, d[k])
if 'user_code' not in d:
self.user_code = re.sub(
r"^(\s*?)(\w.*?\s*?#\s*?yourcodehere.*?)$",
r"\1# your code here",
self.user_code_reference,
flags = re.M,
)
if not hasattr(self, 'test_cases'):
self.test_cases = ''
if not hasattr(self, 'test_cases_submit'):
self.test_cases_submit = ''
def eval_test(self, user_code, test_input):
test_input = s2b(test_input)
if test_input:
# reference
rout, rerr = run_python_code(self.user_code_reference, test_input, online=False)
if rerr: raise Exception(b2s(rout+rerr))
# user
uout, uerr = run_python_code(user_code, test_input, online=self.force_online)
if uerr: raise Exception(b2s(uout+uerr))
return f'期待输出:\n{b2s(rout)}{"-"*10}\n实际输出:\n{b2s(uout)}'
else:
# user
uout, uerr = run_python_code(user_code, test_input, online=self.force_online)
if uerr: raise Exception(b2s(uout+uerr))
return b2s(uout)
def eval_submit(self, user_code):
test_input = self.test_cases_submit
test_input = s2b(test_input)
# reference
rout, rerr = run_python_code(self.user_code_reference, test_input, online=False)
if rerr: raise Exception(b2s(rout+rerr))
# user
uout, uerr = run_python_code(user_code, test_input, online=self.force_online)
if uerr: raise Exception(b2s(uout+uerr))
if uout==rout:
return '提交通过'
else:
raise Exception(f'程序成功运行,但输出与预期不符')
#--------
# def eval_somecode(self, user_code, stdin='', online=False):
# totalcode = self.before_code +'\n'+user_code+'\n'+self.after_code
# err, result = run_python_code(totalcode, stdin_text=stdin, use_tio=online)
# return err, result
#
# def eval_submission(self, user_code):
# ref_err, ref_res = self.eval_somecode(
# self.user_code_reference, online=False)
# if ref_err:
# print('Error running user_code_reference')
# raise Exception(ref_err)
#
# inq_err, inq_res = self.eval_somecode(user_code,
# online=self.force_online)
# return ref_err, ref_res, inq_err, inq_res
#
# def eval_test(self, user_code):
# inq_err, inq_res = self.eval_somecode(user_code,
# online=self.force_online)
# return inq_err, inq_res
#---------
@lru_cache()
def get_preprocessor(self):
return self.exec_code(self.test_case_preprocessor, 'eat')
@lru_cache()
def exec_code(self, code, want):
globals = {}
locals = {}
exec(code, globals, locals)
if want not in locals:
raise Exception(f'所提交的代码中找不到 {want}')
return locals[want]
@lru_cache()
def test_cases_from_string(self, s):
cases = []
eat = self.get_preprocessor()
for i in s.split('\n'):
try:
res = eat(i)
except Exception as e:
print_err(e)
raise Exception(f'输入格式不合法: "{i}"')
else:
if res:
cases.append(res)
return cases
def eval_solution_against_cases(self, solution_code, cases):
Solution = self.exec_code(solution_code,'Solution')
if not inspect.isclass(Solution):
raise Exception('Solution is not a class')
meths = inspect.getmembers(Solution, predicate=inspect.isfunction)
if not meths:
raise Exception('Solution has no methods')
target_function = meths[0]
outputs = []
for idx, case in enumerate(cases):
try:
res = target_function[1](Solution, *case)
except Exception as e:
raise Exception(
str(e)+f'\ntest case {idx+1}/{len(case)}({case})got an error')
else:
# print(case, res)
outputs.append(res)
return outputs
def take_user_test(self, code, cases_string):
cases = self.test_cases_from_string(cases_string)
user_code_outputs = self.eval_solution_against_cases(code, cases)
ref_outputs = self.eval_solution_against_cases(
self.user_code_reference, cases)
print = Printer()
for idx, case, uo, ro in zip(range(len(cases)), cases, user_code_outputs, ref_outputs):
case_str = list(case).map(str).join(",")
print(f'测试用例 ({idx+1}/{len(cases)}) 输入: {case_str}')
sign = '√' if ro==uo else '×'
print(f'期望: {ro} 输出: {uo} {sign}')
return print.text
def take_user_submission(self, code):
cases = self.test_cases_from_string(self.test_cases_submit)
user_code_outputs = self.eval_solution_against_cases(code, cases)
ref_outputs = self.eval_solution_against_cases(
self.user_code_reference, cases)
print = Printer()
for idx, case, uo, ro in zip(range(len(cases)), cases, user_code_outputs, ref_outputs):
case_str = list(case).map(str).join(",")
if ro!=uo:
print(f'测试用例 ({idx+1}/{len(cases)}) 未通过\n输入 {case_str} 期望: {ro} 输出: {uo}')
return True, print.text
print(f'测试用例({len(cases)}/{len(cases)}) 全部通过.')
return False, print.text
# run_python_code()
# def take_user_test_online(self, code, cases_string):
# import base64,pickle
#
# def f():
# import base64,pickle
# packed = input()
# packed = base64.decode(packed)
# packed = pickle.loads()
#
# packed = base64.encode(pickle.dumps([f, self, code, cases_string]))
if __name__ == '__main__':
lc = LeetChallenge(False)
lc.user_code_reference = '''
class Solution:
def square(self,a):
return a*a
'''
lc.test_cases = '''
1
2
3
'''
lc.test_cases_submit = '''
998
999
1000
'''
lc.test_case_preprocessor = '''
def eat(s): # function should return tuples used as function params
s = s.strip()
if s: return (int(s),)
return False
'''
# lc.
# print(lc.test_cases_from_string(lc.test_cases))
# lc.reference_eval(lc.test_cases_from_string(lc.test_cases))
print(lc.take_user_test(lc.user_code_reference, lc.test_cases))
print(lc.take_user_submission(lc.user_code_reference))
print(lc.take_user_test(lc.user_code_reference.replace('*','+'), lc.test_cases))
print(lc.take_user_submission(lc.user_code_reference.replace('*','+')))
class LeetChallenges:
def __init__(self, dirname):
fns = os.listdir(dirname).filter(lambda l:l.endswith('.md'))
fullfns = fns.map(lambda l:os.path.abspath(dirname+'/'+l))
self.l = []
self.d = {}
# print(fns, fullfns)
for fn, fullfn in zip(fns, fullfns):
print_up(fn, fullfn)
lc = LeetChallenge(fullfn)
forefn = fn.split('_')[0]
self.l.append(forefn)
self.d[forefn] = lc
self.l.sort()
# lcs = LeetChallenges(os.path.dirname(__file__)+'./leet_challenges')
# if __name__ == '__main__':
# print(lcs.l)