OO_hw13_test

OO_hw13_test

Charles Lv7

简易hw13程序对拍器分享

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

但由于本身输入的各种限制,生成数据变得非常困难,所以我们需要遵守以下几条异常处理来适配数据生成器:

1.returned
如果没有该书,则忽略该操作
2.smeared
如果没有该书,则忽略该操作
smeared同一本书,则忽略该操作
3.lost
如果没有该书,则忽略该操作,
如果书smeared了,则忽略该操作

数据生成器:

hw13.jar
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
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

public class Main {
private static HashMap<String, Integer> map = new HashMap<>();
private static HashMap<String, Boolean> books = new HashMap<>();
private static String lastTime = "[2023-01-01]";
private static HashMap<String, ArrayList<String>> students = new HashMap<>();
private static HashMap<String, ArrayList<String>> orders = new HashMap<>();
private static HashMap<String, HashMap<String, Boolean>> goodBook = new HashMap<>();
private static ArrayList<String> ids;
private static ArrayList<String> library;

public static void main(String[] args) throws ParseException {
int times = (int) (Math.random() * 90 + 10);
System.out.println(times);
for (int i = 0; i < times; i++) {
int type = 'A' + (int) (Math.random() * 2) + 1;
int index = (int) (Math.random() * 9999);
String book = String.format("%c", type) + "-" +
String.format("%04d", index);
while (books.containsKey(book)) {
type = 'A' + (int) (Math.random() * 2) + 1;
index = (int) (Math.random() * 9999);
book = String.format("%c", type) + "-" +
String.format("%04d", index);
}
books.put(book, true);
int book_num = (int) (Math.random() * 4 + 1);
map.put(book, book_num);
ArrayList<String> order = new ArrayList<>();
orders.put(book, order);
System.out.println(book + " " +
book_num);
}
for (int i = 0; i < times / 3; i++) {
int id = (int) (Math.random() * 99999998 + 1);
String stu = String.format("%08d", id);
if (!students.containsKey(stu)) {
ArrayList<String> books = new ArrayList<>();
HashMap<String, Boolean> goods = new HashMap<>();
students.put(stu, books);
goodBook.put(stu, goods);
} else {
times--;
}
}
library = new ArrayList<>(map.keySet());
ids = new ArrayList<>(students.keySet());
int ops = 3 * times;
System.out.println(ops);
SimpleDateFormat sdf = new SimpleDateFormat("[yyyy-MM-dd]");
for (int i = 0; i < ops; i++) {
Date d1 = sdf.parse(lastTime);
d1.setTime(d1.getTime() + ((int) (Math.random() * 3 * 3600 * 24 * 1000)));
String present = sdf.format(d1);
System.out.println(getOp(present));
lastTime = present;
}
}


private static String getStu() {
return ids.get((int) (Math.random() * ids.size()));
}

private static String getOp(String present) {
int orders = (int) (Math.random() * 3) + 1;
if (orders == 1) {
String stu = getStu();
if (!checkEmpty(stu)) {
String book = getSmearedBook(stu);
if (!checkSmeared(stu, book)) {
return String.format(present + " " + stu + " smeared " + book);
} else {
return String.format(present + " " + stu + " borrowed " + getBook(stu));
}
} else {
return String.format(present + " " + stu + " borrowed " + getBook(stu));
}
} else if (orders == 2) {
String stu = getStu();
if (!checkEmpty(stu)) {
String book = LostBook(stu);
if (!checkSmeared(stu, book)) {
return String.format(present + " " + stu + " lost " + book);
} else {
return String.format(present + " " + stu + " borrowed " + getBook(stu));
}
} else {
return String.format(present + " " + stu + " borrowed " + getBook(stu));
}
} else {
String stu = getStu();
if (!checkEmpty(stu)) {
String book = getReturnBook(stu);
return String.format(present + " " + stu + " returned " + book);
} else {
return String.format(present + " " + stu + " borrowed " + getBook(stu));
}
}
}

private static boolean checkSmeared(String stu, String book) {
return (!goodBook.get(stu).get(book)) && students.get(stu).contains(book);
}

private static String LostBook(String stu) {
String book = students.get(stu).get((int) (Math.random() * (students.get(stu).size())));
students.get(stu).remove(book);
goodBook.get(stu).put(book, true);
return book;
}

private static boolean checkEmpty(String stu) {
return students.get(stu).isEmpty();
}

private static String getReturnBook(String stu) {
String book = students.get(stu).get((int) (Math.random() * (students.get(stu).size())));
students.get(stu).remove(book);
goodBook.get(stu).remove(book);
if (orders.get(book).isEmpty()) {
map.put(book, map.get(book) + 1);
} else {
String other = orders.get(book).get(0);
students.get(other).add(book);
}
return book;
}

private static String getSmearedBook(String stu) {
String book = students.get(stu).get((int) (Math.random() * (students.get(stu).size())));
goodBook.get(stu).put(book, true);
return book;
}

private static String getBook(String stu) {
String book = library.get((int) (Math.random() * (library.size())));
while (students.get(stu).contains(book)) {
book = library.get((int) (Math.random() * (library.size())));
}
if (map.get(book) >= 1) {
students.get(stu).add(book);
map.put(book, map.get(book) - 1);
} else {
orders.get(book).add(stu);
}
goodBook.get(stu).put(book, false);
return book;
}
}

总测试程序:

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 = ['Charles','gx','dt','sj','li','wang']
playerName = ['Charles','dt','gx']
# playerName = ['Charles','dt']
work_dir='D:\\coding_file\\study\\Lesson\\oo_lesson\\OO\\test\\hw13'
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('C:\\Users\\charles\\.jdks\\corretto-1.8.0_362-1\\bin\\java.exe -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('java -jar hw13.jar > 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_hw13_test
  • Author: Charles
  • Created at : 2023-05-27 11:45:14
  • Updated at : 2023-05-28 13:58:07
  • Link: https://charles2530.github.io/2023/05/27/oo-hw13-test/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments