-
Notifications
You must be signed in to change notification settings - Fork 0
/
Judger.py
96 lines (76 loc) · 2.59 KB
/
Judger.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
import json
import imp
import time
from pprint import pprint
class Judger:
def __init__(self, name="data.json", debug=True,
save=False, clean_after_read=False):
self.data_path = name
self.debug = debug
self.save = save
self.clean_after_read = clean_after_read
self.runtime_acc = 0
self.runtime_from = 0;
def initTime(self):
self.runtime_acc = 0
self.resetTime()
def resetTime(self):
self.runtime_from = time.time()
def updateTime(self):
self.runtime_acc += (time.time() - self.runtime_from) * 1000
self.resetTime();
def debugPrint(self, *args, pretty=False):
if self.debug:
if pretty:
pprint(*args)
else:
print(*args)
def judgeCase(self, case):
status = []
for sample in case:
now = {}
# main run
self.initTime()
try:
out = self.run(sample)
self.updateTime()
now['time'] = self.runtime_acc
except Exception as e:
self.updateTime()
now['time'] = self.runtime_acc
now["status"] = 'RE'
status.append(now)
if self.debug:
raise
continue
# compare
if not self.compare(out, sample):
now['status'] = "WA"
else:
now['status'] = "AC"
status.append(now)
return status
def judge(self):
data = json.load(open(self.data_path))
if self.clean_after_read:
open(self.data_path, "w").close()
scores = {}
for case in data:
casename = f"case{case['case']}"
self.debugPrint("Case:", casename)
case_status = self.judgeCase(case['data'])
times = sum(i['time'] for i in case_status)
isAC = all(i['status'] == "AC" for i in case_status)
score = case['score'] if isAC else 0
scores[casename] = score
for i, co in enumerate(case_status):
self.debugPrint(f"\tSample{i}:\t{co['status']}\t{co['time']:.2f}ms")
self.debugPrint(f"Score: {score} / {case['score']}")
self.debugPrint(f"Total: {times:.2f}ms")
if self.save:
json.dump(case_status, open(casename + ".out", "w"))
self.debugPrint(json.dumps({'scores': scores}))
def run(self, sample):
return 0
def compare(self, out, sample):
return True