-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashcode2020.py
119 lines (95 loc) · 3.41 KB
/
hashcode2020.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
import itertools
from operator import itemgetter
# Primera linea
from urllib3.connectionpool import xrange
total_books = 0
total_libraries = 0
total_days = 0
# Segona linea
books_scores = []
# Ter
libraries = []
library = {'number_books': 0, 'signup_time': 0, 'ship_limit': 0, 'books': [0, 1, 2]}
sorted_libraries = []
sorted_solution_libraries = []
def read_file(fname):
global total_books
global total_libraries
global total_days
global books_scores
global libraries
with open(fname) as f:
for i, l in enumerate(f):
if i == 0:
total_books = int(l.split()[0])
total_libraries = int(l.split()[1])
total_days = int(l.split()[2])
elif i == 1:
books_scores = l.split()
elif i % 2 == 0:
number_books = l.split()[0]
signup_time = l.split()[1]
ship_limit = l.split()[2]
libraries.append(
{'number_books': number_books, 'signup_time': signup_time, 'ship_limit': ship_limit, 'books': []})
elif i % 2 != 0:
libraries[len(libraries) - 1]['books'] = l.split()
def logic():
global sorted_solution_libraries
global sorted_libraries
for library in libraries:
for i, book in enumerate(library['books']):
library['books'][i] = (book, books_scores[int(book)])
sorted_library = sorted(library['books'], key=lambda x: x[1], reverse=True)
sorted_libraries.append(sorted_library)
print(sorted_libraries)
seen = set()
repeated = set()
for l in sorted_libraries:
for i in set(l):
if i in seen:
repeated.add(i)
else:
seen.add(i)
print("repeated: " + str(repeated))
for a, element_to_remove in enumerate(repeated):
for i in [x for x in xrange(len(sorted_libraries)) if x != a]:
if element_to_remove in sorted_libraries[i]:
sorted_libraries[i].remove(element_to_remove)
solution_libraries = []
for i, library in enumerate(sorted_libraries):
if len(library) > 0:
solution_libraries.append((library, libraries[i]['signup_time'], i))
print("solution libraries: " + str(solution_libraries))
sorted_solution_libraries = sorted(solution_libraries, key=lambda x: x[1])
print("-------------------------")
print("sorted_solution_libraries: " + str(sorted_solution_libraries))
def write_solution(namefile):
f = open(namefile + "_output.txt", "w")
f.write(str(len(sorted_solution_libraries)) + '\n')
for library in sorted_solution_libraries:
f.write(str(library[2]) + " " + str(len(library[0])) + '\n')
for a in library[0]:
f.write(str(a[0]) + ' ')
f.write('\n')
f.close()
def main():
#file = "a_example"
#file = "b_read_on"
file = "c_incunabula"
#file = "d_tough_choices"
#file = "e_so_many_books"
#file = "f_libraries_of_the_world"
read_file("problem/" + file + ".txt")
logic()
print("total_books: " + str(total_books))
print("total_libraries: " + str(total_libraries))
print("total_days: " + str(total_days))
print("-----------------------")
print("books_scores: " + str(books_scores))
print(libraries)
print("-----------------------")
print(sorted_libraries)
write_solution(file)
if __name__ == "__main__":
main()