太棒了,我用Python做了一个简单的OI测评器(不需要任何第三方模块)!ㄟ(≧◇≦)ㄏ

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,大家可以试着加一下)

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值