-
Notifications
You must be signed in to change notification settings - Fork 24
/
record.py
190 lines (157 loc) · 6.22 KB
/
record.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
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
from itertools import chain
from sys import stderr
import util
__school_penalty__ = {
0: 0,
1: -40,
2: 60,
3: 120,
4: 180,
5: 300,
}
__contest_type_map__ = {
"CSP入门": "CSP",
"CSP提高": "CSP",
"NOIP普及": "NOIP",
"NOIP提高": "NOIP",
"NOI": "NOI",
"NOID类": "NOI",
}
class Record:
__auto_increment__ = 0
def __init__(self, oier, contest, score, rank, level, grades, school, province, gender):
Record.__auto_increment__ += 1
self.id = Record.__auto_increment__
self.oier = oier
self.contest = contest
self.score = score
self.rank = rank
self.level = level
self.grades = grades
self.school = school
self.province = province
self.gender = gender
self.ems = util.enrollment_middle(contest, grades)
self.keep_grade_flag = False
def __repr__(self):
return f"{self.oier.name}(pro={self.province},school={self.school.name},ems={self.ems},c={self.contest.name})"
@staticmethod
def __score_format__(score):
return "" if score is None else f"{score:.5g}"
@staticmethod
def __province_format__(province):
try:
return util.provinces.index(province)
except:
print(
f"\x1b[01;33mwarning: \x1b[0m未知的省级行政区:\x1b[0;32m'{province}'\x1b[0m",
file=stderr,
)
return province
@staticmethod
def __award_level_format__(level):
try:
return util.award_levels.index(level)
except:
print(
f"\x1b[01;33mwarning: \x1b[0m未知的奖项名称:\x1b[0;32m'{level}'\x1b[0m",
file=stderr,
)
return level
def to_compress_format(self, reference_em):
"转化成压缩格式字符串。"
s = "{}:{}:{}:{}:{}:{}".format(
self.contest.id,
self.school.id,
Record.__score_format__(self.score),
self.rank,
Record.__province_format__(self.province),
Record.__award_level_format__(self.level),
)
if self.keep_grade_flag:
s += ";" + str(util.get_weighted_mode([self.ems])[0])
elif reference_em not in self.ems:
s += ":" + str(util.get_weighted_mode([self.ems])[0])
return s
def is_keep_grade(self):
"获取该记录组是否为非正常年级 (如留级)。"
return self.keep_grade_flag
def keep_grade(self):
"标记一个记录组为非正常年级 (如留级),显示时保留年级。"
self.keep_grade_flag = True
@staticmethod
def distance(A, B, inf=2147483647):
"""获取两个记录组的距离。
A: 第一个记录组。
B: 第二个记录组。
"""
assert len(A) and len(B)
# 年份差异过大,视作不同的记录组
# - 10 年以上的默认拆分,如有特例手动合并即可
# - 显然在可以预见的将来,特例的情况比错误合并的情况会少得多
min_year = min(record.contest.year for record in chain(A, B))
max_year = max(record.contest.year for record in chain(A, B))
if max_year - min_year > 9:
return inf
for a in A:
for b in B:
if a.contest is b.contest:
return inf
if abs(a.gender - b.gender) == 2:
return inf
if a.contest.school_year() == b.contest.school_year():
if len(set(a.ems) & set(b.ems)) == 0:
return inf
# 在同一年中有不同参赛学校的同类赛事的,不合并
if (
a.contest.type in __contest_type_map__
and b.contest.type in __contest_type_map__
and __contest_type_map__[a.contest.type] == __contest_type_map__[b.contest.type]
and a.school.name != b.school.name
):
return inf
# 在高中毕业后又有小学记录的情况下,不应该合并
if (
a.contest.school_year() < b.contest.school_year()
and any(level in a.school.name for level in ["高中", "中学", "高级"])
and "小学" not in a.school.name
and "小学" in b.school.name
):
return inf
schools = set(record.school.id for record in chain(A, B))
locations = set(record.school.location() for record in chain(A, B))
provinces = set(record.province for record in chain(A, B))
aem = util.get_mode([record.ems for record in A])
bem = util.get_mode([record.ems for record in B])
diff = min(abs(i - j) for i in aem for j in bem)
return (
__school_penalty__.get(len(schools), 600)
+ 80 * (len(locations) + len(provinces) - 3)
+ 100 * diff
)
@staticmethod
def check_stay_down(A, B):
"""检测两个记录组的合并是否是因为留级等现象 (需要保留年级)。
A: 第一个记录组。
B: 第二个记录组。
返回值: 0 表示非留级现象,1 表示 B 是留级后的记录组,-1 表示 A 是留级后的记录组。
"""
assert len(A) and len(B)
emsA = set(chain(*[record.ems.keys() for record in A]))
emsB = set(chain(*[record.ems.keys() for record in B]))
if not (len(emsA) == 1 and len(emsB) == 1 and len(A) > 1 and len(B) > 1):
return 0
aem, bem = emsA.pop(), emsB.pop()
if abs(aem - bem) != 1:
return 0
if len(A) < 2 if aem + 1 == bem else len(B) < 2:
return 0
schools = set(record.school.id for record in chain(A, B))
locations = set(record.school.location() for record in chain(A, B))
provinces = set(record.province for record in chain(A, B))
penalty = __school_penalty__.get(len(schools), 600) + 80 * (len(locations) + len(provinces) - 3)
if penalty >= 100:
return 0
return bem - aem