简介

支持数据生成程序模式, 只要有RE或者WA的数据点, 就会停止

支持数据文件模式, 使用通配符指定输入文件, 将会对拍所有文件

结束后将会打印统计信息

第一次在某目录执行,将会通过交互方式获取配置, 生成配置文件, 以后读取配置文件运行

支持Linux和Windows系统

代码

#!/usr/bin/python3
import os
import json
import platform
import time
import glob
import shutil def confirm(msg, helpi=''):
while True:
cmd = input("{} [yes]/no{} : ".format(msg, "/help" if helpi else ''))
if cmd == "no":
return False
elif cmd == "help" and helpi:
print(helpi)
else:
return True def getinput(msg, helpi=''):
while True:
cmd = input("{} {} : ".format(msg, "<input>/help" if helpi else ''))
if cmd == "help" and helpi:
print()
print(helpi)
print()
elif cmd:
return cmd settings = {}
if not os.path.isfile("checkx.json"):
try:
print("未找到checkx.json配置文件\n")
settings['std'] = getinput(
"标准程序源代码路径", "1. 不需要使用freopen\n2. 可以使用绝对路径或相对路径")
settings['target'] = getinput(
"测试程序源代码路径", "1. 不需要使用freopen\n2. 可以使用绝对路径或相对路径")
settings['ifpro'] = confirm(
"是否使用数据生成程序?", "若输入yes, 将会使用数据生成程序生成数据\n若输入no, 将会使用数据文件")
if settings['ifpro']:
settings['pro'] = getinput("数据生成程序路径", "源程序应该直接输出数据")
else:
settings['file'] = getinput(
"数据文件路径", "1. 可以使用绝对路径或相对路径\n2. 支持通配符")
with open("checkx.json", "w") as f:
f.write(json.dumps(settings))
except KeyboardInterrupt:
exit(0)
else:
with open("checkx.json", "r") as f:
settings = json.loads(f.read())
print("正在编译标准程序...", end='')
os.system("g++ {} -DDEBUG -o {}.out".format(settings['std'], settings['std']))
print("编译完成\n正在编译测试程序...", end='')
os.system(
"g++ {} -DDEBUG -o {}.out".format(settings['target'], settings['target']))
print("编译完成")
if "Windows" in platform.platform():
diffcmd = "fc /w target.out std.out > diff.out"
else:
diffcmd = "diff -ZE target.out std.out > diff.out"
count = 0
times = []
stdtimes = []
account = 0
if settings['ifpro']:
try:
while True:
count += 1
os.system(".{}{} > in.in".format(os.sep, settings['pro']))
zerotime = time.time()
os.system(".{}{} < in.in > std.out".format(os.sep, settings['std']+".out"))
begin = time.time()
stdtimes.append(begin-zerotime)
if not os.system("{} < in.in > target.out".format(settings['target']+".out")):
print()
print("# {} Runtime Error".format(count))
print("\n输入:\n")
with open("in.in", "r") as f:
print(f.read())
break
end = time.time()
times.append(end-begin)
if os.system(diffcmd):
print()
print("Wrong Answer")
print("\n输入:\n")
with open("in.in", "r") as f:
print(f.read())
print("\n标准程序输出:\n")
with open("std.out", "r") as f:
print(f.read())
print("\n测试程序输出:\n")
with open("target.out", "r") as f:
print(f.read())
print("\n差异比较:\n")
with open("diff.out", "r") as f:
print(f.read())
break
else:
account += 1
print("\r# {} Accepted {}s".format(
count, round(end-begin, 2)), end='')
except KeyboardInterrupt:
pass
else:
try:
for i in glob.glob(settings['file']):
count += 1
zerotime = time.time()
os.system(".{}{} < {} > std.out".format(os.sep, settings['std']+".out", i))
begin = time.time()
stdtimes.append(begin-zerotime)
if not os.system(".{}{} < {} > target.out".format(os.sep, settings['target']+".out", i)):
print("# {} Runtime Error".format(count))
print("\n输入保存在文件 result{}.in\n".format(count))
shutil.copy(i, "result{}.in".format(count))
continue
end = time.time()
times.append(end-begin)
if os.system(diffcmd):
print("# {} Wrong Answer".format(count))
print("\n输入保存在文件result{}.in\n".format(count))
shutil.copy(i, "result{}.in".format(count))
print("\n标准程序输出保存在文件 result{}.ans\n".format(count))
shutil.copy("std.out", "result{}.out".format(count))
print("\n测试程序输出保存在文件 result{}.out\n".format(count))
shutil.copy("target.out", "result{}.ans".format(count))
print("\n差异比较保存在文件 result{}.diff\n".format(count))
shutil.copy("diff.out", "result{}.diff".format(count))
else:
account += 1
print("# {} Accepted {}s".format(count, round(end-begin, 2)))
except KeyboardInterrupt:
pass
print("\n共测试 {} 个数据点".format(count))
if count:
print("{} 个数据点正确".format(account))
print("正确率 {} %".format(round(account/count*100, 2)))
if count:
print("标准程序在正确的数据点中:\n\t平均时间 {} s".format(round(sum(stdtimes)/count, 2)))
print("\t最长时间 {} s".format(round(max(stdtimes), 2)))
print("\t最短时间 {} s".format(round(min(stdtimes), 2)))
if account:
print("测试程序在正确的数据点中:\n\t平均时间 {} s".format(round(sum(times)/account, 2)))
print("\t最长时间 {} s".format(round(max(times), 2)))
print("\t最短时间 {} s".format(round(min(times), 2)))
os.remove("diff.out")
os.remove("std.out")
os.remove("target.out")
os.remove(settings['std']+".out")
os.remove(settings['target']+".out")
if settings['ifpro']:
os.remove("in.in")

使用Python编写的对拍程序的更多相关文章

  1. pyinstaller将python编写的打卡程序打包成exe

    编写了一个简易的定时提醒下班打卡程序,python代码如下: #coding:utf-8 import time import datetime from tkMessageBox import * ...

  2. 使用Python编写打字训练小程序【华为云技术分享】

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/devcloud/article/detail ...

  3. 使用Python编写打字训练小程序

    你眼中的程序猿 别人眼中的程序猿,是什么样子?打字如飞,各种炫酷的页面切换,一个个好似黑客般的网站破解.可现实呢? 二指禅的敲键盘,写一行代码,查半天百度-那么如何能让我们从外表上变得更像一个程序猿呢 ...

  4. Python编写的记事本小程序

    用Python中的Tkinter模块写的一个简单的记事本程序,Python2.x和Python3.x的许多内置函数有所改变,所以以下分为Python2.x和Python3.x版本. 一.效果展示: 二 ...

  5. 如何用python编写一个计时器的程序

    python计时器的程序的代码和注释 import time as t #引入time模块 class MyTimer():     def __init__(self): #构造函数         ...

  6. 基于python编写的天气抓取程序

    以前一直使用中国天气网的天气预报组件都挺好,可是自从他们升级组件后数据加载变得非常不稳定,因为JS的阻塞常常导致网站打开速度很慢.为了解决这个问题决定现学现用python编写一个抓取程序,每天定时抓取 ...

  7. 一起学Hadoop——使用IDEA编写第一个MapReduce程序(Java和Python)

    上一篇我们学习了MapReduce的原理,今天我们使用代码来加深对MapReduce原理的理解. wordcount是Hadoop入门的经典例子,我们也不能免俗,也使用这个例子作为学习Hadoop的第 ...

  8. python 之 Django框架(服务器程序和应用程序、基础必备三件套及相关配置)

    第十二章 Django框架 12.1 服务器程序和应用程序 服务器程序负责对socket服务器进行封装,并在请求到来时,对请求的各种数据进行整理.应用程序则负责具体的逻辑处理.为了方便应用程序的开发, ...

  9. 【转载】Python编写简易木马程序

    转载来自: http://drops.wooyun.org/papers/4751?utm_source=tuicool 使用Python编写一个具有键盘记录.截屏以及通信功能的简易木马. 首先准备好 ...

随机推荐

  1. Python数据分析之双色球高频数据统计

    Step1:基础数据准备(通过爬虫获取到),以下是从第一期03年双色球开奖号到今天的所有数据整理,截止目前一共2549期,balls.txt 文件内容如下 : 备注:想要现成数据的可以给我发邮件哟~ ...

  2. java关键字tranisent用法详解

    作为java中的一个关键字,tranisent用的并不是很多,但是在某些关键场合,却又起着极为重要的作用,因此有必要对它进行一些必要的了解. 一.定义:声明不用序列化的成员域.(源自百度百科) 二.作 ...

  3. 给Linux小白的CentOS8.1基本安装说明

    写在前面的话:用过Linux的同学应该都会觉得Linux安装是件非常简单的事情,根本不值得用博客记下来!但是我发现,其实没接触过Linux的同学还真不一定会装,就像在IT行业工作过几年但一直用Wind ...

  4. Linux系统如何设置开机自动运行脚本?

    大家好,我是良许. 在工作中,我们经常有个需求,那就是在系统启动之后,自动启动某个脚本或服务.在 Windows 下,我们有很多方法可以设置开机启动,但在 Linux 系统下我们需要如何操作呢? Li ...

  5. 关于thinkphp5下URL附加参数,无法获取到(?参数)

    nginx 配置问题: 修改配置后:

  6. Arduino+sim800C家居安防火灾报警 拨打电话 发送短信例程程序

    家居安防报警器,参考程序. 火灾报警 涉及用sim800c发短信,拨打电话通知. 接线: Sim800c 3.3V -> Arduino 3.3V Sim800c GND -> Ardui ...

  7. Probius:一个功能强大的自定义任务系统

    断更的这些日子,我又折腾了一个轮子,文末参考源码 大约在一年半以前写过一篇文章『探秘varian:优雅的发布部署程序』,里边有讲到我们采用类似lego的模块化方式来构建CICD的流程,虽能满足我们的需 ...

  8. 深入理解Java虚拟机学习笔记(三)-----类文件结构/虚拟机类加载机制

    第6章 类文件结构 1. 无关性 各种不同平台的虚拟机与所有平台都统一使用的程序存储格式——字节码(即扩展名为 .class 的文件) 是构成平台无关性的基石. 字节码(即扩展名为 .class 的文 ...

  9. Mac 安装fiddler

    1, 安装mono 2,下载fiddler for mac https://www.telerik.com/download/fiddler 3. 解压fiddler-mac.zip 4, cd fi ...

  10. MOJITO 发布一周,爬一波弹幕分析下

    MOJITO 最近一直啥都没写,追个热点都赶不上热乎的,鄙视自己一下. 周董的新歌 「MOJITO」 发售(6 月 12 日的零点)至今大致过去了一周,翻开 B 站 MV 一看,播放量妥妥破千万,弹幕 ...