OO_hw9_test

OO_hw9_test

Charles Lv7

简易hw9程序对拍器分享

第三单元作业还是比较简单的,甚至连对拍器都不用写,因为大家的输出可以直接通过文本比较。

数据生成器:

generate.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
import random
import sys
import names

instr_list = ['ap', 'ar', 'qv', 'qci', 'qbs','qts']
person_id_set = set()
group_id_set = set()
link_map = {}
group_map = {}
# MAX_INT=2147483648
MAX_INSTR=10000
MAX_INT=9

def get_unexist_id(id_set) :
id = random.randint(-MAX_INT, MAX_INT-1)
while (id in id_set) :
id = random.randint(-MAX_INT, MAX_INT-1)
return str(id)

def get_exist_id(id_set) :
id = random.choice(list(id_set))
return str(id)

def get_name() :
name = names.get_first_name()
while (len(name) > 10) :
name = names.get_first_name()
return name

def get_age() :
age = random.randint(0, 201)
return str(age)

def get_value() :
value = random.randint(1, 101)
return str(value)

def get_instr() :
instr = random.choice(instr_list)
if (instr == 'ap') :
return add_person(instr)
elif (instr == 'ar') :
return add_relation(instr)
elif (instr == 'qv') :
return query_value(instr)
elif (instr == 'qci') :
return query_circle(instr)
elif (instr == 'qbs') :
return instr
elif (instr == 'qts') :
return instr


def add_person(instr) :
prob = random.uniform(0, 1)
id = get_unexist_id(person_id_set)
if (prob < 0.4) :
if (person_id_set) :
id = get_exist_id(person_id_set)
else :
id = get_unexist_id(person_id_set)
person_id_set.add(id)
link_map[id] = []
return instr + " " + id + " " + get_name() + " " + get_age()

def add_relation(instr) :
prob = random.uniform(0, 1)
id1 = get_unexist_id(person_id_set)
id2 = get_unexist_id(person_id_set)
if (prob < 0.2) :
id1 = get_unexist_id(person_id_set)
id2 = str(random.randint(-2147483648, 2147483647))
elif (prob < 0.4) :
if (person_id_set) :
id1 = get_exist_id(person_id_set)
id2 = get_unexist_id(person_id_set)
elif (prob < 0.6) :
if (person_id_set) :
id1 = get_exist_id(person_id_set)
if (link_map[id1]) :
id2 = random.choice(link_map[id1])
else :
id2 = id1

else :
if (person_id_set) :
id1 = get_exist_id(person_id_set)
id2 = get_exist_id(person_id_set)
if (len(link_map[id1]) + 1 < len(person_id_set)) :
while (id2 in link_map[id1]) :
id2 = get_exist_id(person_id_set)
link_map[id1].append(id2)
link_map[id2].append(id1)
return instr + " " + id1 + " " + id2 + " " + get_value()

def query_value(instr) :
prob = random.uniform(0, 1)
id1 = get_unexist_id(person_id_set)
id2 = get_unexist_id(person_id_set)
if (prob < 0.2) :
id1 = get_unexist_id(person_id_set)
id2 = str(random.randint(-2147483648, 2147483647))
elif (prob < 0.4) :
if (person_id_set) :
id1 = get_exist_id(person_id_set)
id2 = get_unexist_id(person_id_set)
elif (prob < 0.6) :
if (person_id_set) :
id1 = get_exist_id(person_id_set)
id2 = get_exist_id(person_id_set)
if (len(link_map[id1]) + 1 < len(person_id_set)) :
while (id2 in link_map[id1]) :
id2 = get_exist_id(person_id_set)
else :
if (person_id_set) :
id1 = get_exist_id(person_id_set)
if (link_map[id1]) :
id2 = random.choice(link_map[id1])
else :
id2 = id1
return instr + " " + id1 + " " + id2

def query_circle(instr) :
prob = random.uniform(0, 1)
id1 = get_unexist_id(person_id_set)
id2 = get_unexist_id(person_id_set)
if (prob < 0.2) :
id1 = get_unexist_id(person_id_set)
id2 = str(random.randint(-2147483648, 2147483647))
elif (prob < 0.4) :
if (person_id_set) :
id1 = get_exist_id(person_id_set)
id2 = get_unexist_id(person_id_set)
else :
if (person_id_set) :
id1 = get_exist_id(person_id_set)
id2 = get_exist_id(person_id_set)
return instr + " " + id1 + " " + id2

if __name__ == '__main__':
n = random.randint(10,MAX_INSTR)
# n=30
for i in range(n) :
instr = get_instr()
print(instr)

总测试程序:

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import os
import time
import sys
from colorfulPrint import ColorfulPrint

# set names here
# playerName = ['Lancer', 'Archer', 'Rider', 'Caster', 'Assassin', 'Berserker']
playerName = ['Charles','gx','dt']
# playerName = ['Charles','dt']
work_dir='D:\\coding_file\\study\\Lesson\\oo_lesson\\OO\\test\\hw9\\copy_test'
times = []
ans = []
lineNum = []

# set running parameter here
playerNum = len(playerName)
recodeTime = True
displayDetail = True
showTime = True
maxWaCount = 5

def makeLog(num, size, aboutTime = False):
if aboutTime == False:
with open('stdin.txt', 'r') as f:
con = f.read()
with open('./logWA/stdin' + str(num) + '.txt', 'w') as f:
f.write(con)
for i in range(size):
name1 = 'stdout' + str(i + 1) + '.txt'
name2 = './logWA/stdout' + str(i + 1) + '_' + str(num) + '.txt'
with open(name1, 'r') as f:
con = f.read()
with open(name2, 'w') as f:
f.write(con)
else:
with open('stdin.txt', 'r') as f:
con = f.read()
with open('./logTLE/stdin' + str(num) + '.txt', 'w') as f:
f.write(con)
for i in range(size):
name1 = 'stdout' + str(i + 1) + '.txt'
name2 = './logTLE/stdout' + str(i + 1) + '_' + str(num) + '.txt'
with open(name1, 'r') as f:
con = f.read()
with open(name2, 'w') as f:
f.write(con)

def exec(cmd, display = displayDetail):
if display:
print('Executing: ' + cmd)
os.system(cmd)

def init():
for _ in range(playerNum):
times.append(0.0)
ans.append([])

def runAndGetAns(recodeTime):
for i in range(playerNum):
name = playerName[i]
timeBegin = time.time()
exec('java -jar ' + name + '.jar < stdin.txt > '+name+'_stdout.txt')
timeEnd = time.time()
times[i] = timeEnd - timeBegin
with open(name+'_stdout.txt', 'r') as f:
ans[i] = f.readlines()
if recodeTime:
temp = ''
for i in range(playerNum):
temp += str(times[i]) + ','
temp = temp[:-1]
temp += '\n'
with open('time.txt', 'a') as f:
f.write(temp)

def check():
waCount = 0
flag = True
for i in range(len(ans[0])):
for j in range(playerNum):
if j == 0:
cmp = ans[j][i]
else:
if cmp != ans[j][i]:
flag = False
ColorfulPrint.colorfulPrint('Error in line: ' + str(i + 1) + '!', ColorfulPrint.MODE_BOLD, ColorfulPrint.COLOR_RED)
waCount += 1
if waCount >= maxWaCount:
return False
return flag

if __name__ == '__main__':
os.chdir(work_dir)
init()
for i in range(int(sys.argv[1])):
exec('python generate.py > stdin.txt')
ColorfulPrint.colorfulPrint('>>>>>>>>>> Test ' + str(i + 1) + ' <<<<<<<<<<', ColorfulPrint.MODE_BOLD, ColorfulPrint.COLOR_BLUE)
runAndGetAns(recodeTime)
flag = check()
if flag:
ColorfulPrint.colorfulPrint('===== Accepted =====', ColorfulPrint.MODE_BOLD, ColorfulPrint.COLOR_GREEN)
else:
ColorfulPrint.colorfulPrint('***** Wrong Answer! *****', ColorfulPrint.MODE_BOLD, ColorfulPrint.COLOR_RED)
if showTime:
for i in range(playerNum):
ColorfulPrint.colorfulPrint('>>>>> ' + playerName[i] + ' use time: ' + str(times[i]) + 's <<<<<', ColorfulPrint.MODE_BOLD, ColorfulPrint.COLOR_BLUE)
if flag == False:
makeLog(i + 1, playerNum)
break
assert flag == True

终端渲染包:

colorfulPrint.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
# 支持两种模式:正常(NORMAL)和加粗(BOLD)
# 支持8种颜色:灰色、红色、绿色、黄色、蓝色、粉色、青色、白色
class ColorfulPrint():
MODE_NORMAL = 1
MODE_BOLD = 2
COLOR_GRAY = 0
COLOR_RED = 1
COLOR_GREEN = 2
COLOR_YELLOW = 3
COLOR_BLUE = 4
COLOR_PINK = 5
COLOR_CYAN = 6
COLOR_WHITE = 7
@staticmethod
def colorfulPrint(msg, mode, color):
head = '\033['
if mode == ColorfulPrint.MODE_NORMAL:
head = head + '0;'
elif mode == ColorfulPrint.MODE_BOLD:
head = head + '1;'
else:
print('\033[1;31;40m' + ' ***** MODE ERROR ***** ' + '\033[0m')
return
if 0 <= color <= 7:
head = head + str(30 + color) + ';40m'
else:
print('\033[1;31;40m' + ' ***** COLOR ERROR ***** ' + '\033[0m')
tail = '\033[0m'
print(head + msg + tail)
  • Title: OO_hw9_test
  • Author: Charles
  • Created at : 2023-04-22 22:46:31
  • Updated at : 2023-04-22 22:51:00
  • Link: https://charles2530.github.io/2023/04/22/oo-hw9-test/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments