import subprocess, time
code = '''
''' # Place the test code here
file = open('code.py', 'w')
file.write(code)
file.close()
testdata = {} # Place the test data here
# {<input data: str>: (<correct output data: str>, <timeout seconds: float / int>)}
testdatasum = len(testdata.keys())
corrects = 0
for k, v in testdata.items():
print('Testing...')
open('test.txt', 'w').write(k)
file = open('test.txt')
start = time.time()
timeout = False
proccess = subprocess.Popen(['python', 'code.py'], stdout = open('output.txt', 'w'),
stdin = file,
stderr = open('errors.txt', 'w')
)
while proccess.poll() != 0:
end = time.time()
if end - start > v[1]:
timeout = True
break
proccess.terminate()
output = open('output.txt').read().strip('\n')
errors = open('errors.txt').read().strip('\n')
if timeout:
print('Time Limit Exceed')
elif errors:
print('Compile Error')
elif output != v[0]:
print('Wrong Answer')
else:
print('Accepted')
corrects += 1
open('output.txt', 'w').truncate()
open('errors.txt', 'w').truncate()
print('Score: ' + str(int(corrects / testdatasum * 100)))
这个是我windows10系统的python3.6.4编的程序。
下面是a+bProblem的accepted输出结果:
Testing...
Accepted
Testing...
Accepted
Testing...
Accepted
Score: 100
(当然,我没加output limit exceed,presentation error,memory limit exceed和runtime error,大家可以试着加一下)