-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
146 lines (121 loc) · 3.93 KB
/
utils.py
File metadata and controls
146 lines (121 loc) · 3.93 KB
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 copy
# function used to parse input.
def parseINPUT(file_name):
f = open(file_name, "r")
B, L, D = map(int, f.readline().split(" "))
B_value = list(map(int, f.readline().split(" ")))
# N_n used to store books in each library
N_n = []
T = []
M = []
N = []
for i in range(L):
n, t, m = map(int, f.readline().split(" "))
N_n.append(n)
T.append(t)
M.append(m)
id_list = list(map(int, f.readline().split(" ")))
N.append(id_list)
f.close()
return {
"numOFbooks": B,
"numOFlibs": L,
"numOFdays": D,
"valueOFbook": B_value,
"booksINlib": N,
"numOFsignupDays": T,
"numOFbooksSHIPPED": M,
"numOFbooksINlib": N_n
}
# function to compute the score of the submission.
# The input to the function is limited and it assume that the book all can be shipped legally.
# It is used for fine-tunning
def judgeFunction(output_string, B_value):
# used to store current score can be added by this book.
B_value_cur = copy.deepcopy(B_value)
lines = output_string.split("\n")
A = int(lines[0])
sumBooks = 0
sum_score = 0
for i in range(A):
books = list(map(int, lines[i * 2 + 2].split(" ")))
for book in books:
sum_score += B_value_cur[book]
assert isinstance(B_value_cur[book], int)
if B_value_cur[book] != 0:
sumBooks += 1
B_value_cur[book] = 0
print("number of books is %d" % sumBooks)
return sum_score
def weak_judgeFunction(shippedBooks, B_value):
sum_score = 0
B_value_cur = copy.deepcopy(B_value)
for lib in shippedBooks:
for book in lib:
sum_score += B_value_cur[book]
B_value_cur[book] = 0
return sum_score
def generateSubmission(orderedLib, shippedBooks):
A = len(orderedLib)
output = str(A) + "\n"
for i in range(A):
output += (str(orderedLib[i]) + " " + str(len(shippedBooks[i])) + "\n")
for book in shippedBooks[i]:
output += (str(book) + " ")
output = output[:-1]
output += "\n"
return output
def bubbleSortReversed(inList, cmp, number):
length = len(inList)
assert length >= number
for i in range(number):
for j in range(i + 1, length):
if cmp(inList[i], inList[j]) < 0:
# swap
inList[i], inList[j] = inList[j], inList[i]
def bubbleSortReversedByvalue(inList, value_list, number):
length = len(inList)
assert length >= number
assert len(value_list) == length
for i in range(number):
for j in range(i + 1, length):
if value_list[i] < value_list[j]:
# swap
inList[i], inList[j] = inList[j], inList[i]
value_list[i], value_list[j] = value_list[j], value_list[i]
def bubbleSortReversedBydict(inList, dict, number):
length = len(inList)
assert length >= number
for i in range(number):
for j in range(i + 1, length):
if dict[inList[i]] < dict[inList[j]]:
# swap
inList[i], inList[j] = inList[j], inList[i]
def min(a, b):
if a > b:
return b
else:
return a
def orderedLib2N_n(orderLib, B_value, T, M, N_n, N, D):
B_valueCur = copy.deepcopy(B_value)
curDay = 0
shippedBooks = []
for lib in orderLib:
sumBooks = min((D - curDay - T[lib]) * M[lib], N_n[lib])
ship = []
if sumBooks < N_n[lib]:
time = 0
for book in N[lib]:
if time >= sumBooks:
break
if B_valueCur[book] != 0:
ship.append(book)
B_valueCur[book] = 0
time += 1
else:
ship = N[lib]
for book in N[lib]:
B_valueCur[book] = 0
shippedBooks += [ship]
curDay += T[lib]
return shippedBooks