-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgo-biblio.py
99 lines (77 loc) · 2.26 KB
/
algo-biblio.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
#/usr/bin/python3
from clases import *
import functools
from parsers import parse, escribir_resultado
import sys
import time
BOOKS = {
'a': 'a_example.txt',
'b': 'b_read_on.txt',
'c': 'c_incunabula.txt',
'd': 'd_tough_choices.txt',
'e': 'e_so_many_books.txt',
'f': 'f_libraries_of_the_world.txt',
}
numero_libros = []
tiempos_registro = {}
in_file = 'input_data/' + BOOKS[sys.argv[1]]
out_file = 'output_data/' + BOOKS[sys.argv[1]]
print(in_file)
books, libraries, days = parse(in_file)
import pickle
with open('libros.pkl', 'wb') as f:
pickle.dump(books, f)
with open('libraries.pkl', 'wb') as f:
pickle.dump(libraries, f)
print('volcado')
def mejor_biblio(libraries, scanned):
puntuaciones = []
best_library = libraries[0]
rest = []
best_score = 0
print('scanned:', len(scanned))
for library in libraries[1:]:
score = library.getScore(scanned)
if score > best_score:
best_score = score
rest.append(best_library)
best_library = library
else:
rest.append(library)
return best_library, rest
# puntuaciones = list(map(calcular_puntuacion, libraries)).sort()
# return puntuaciones[-1]
def orden_libs_fn(l1, l2):
if l1.treg < l2.treg:
return -1
elif l1.treg > l2.treg:
return 1
else:
if l1.rate > l2.rate:
return -1
elif l1.rate < l2.rate:
return 1
else:
if len(l1.books) > len(l2.books):
return -1
elif len(l1.books) < len(l2.books):
return 1
else:
return 1
# hay que registrar primero las bibliotecas que tardan menos en 'registrarse'
salida = []
curr_days = 0
libros_ya_escaneados = set()
while len(libraries) > 0 and curr_days <= days:
print('=====================')
init = time.time()
best_lib, libraries = mejor_biblio(libraries, libros_ya_escaneados)
print(len(libraries), '---')
print('Mejor biblio:', best_lib)
print('Tiempo de procesado:', time.time() - init)
libros = best_lib.scan_books()
libros_ya_escaneados.update(libros)
salida.append((best_lib, libros))
curr_days += 1
print(f'Finish. day {curr_days}')
escribir_resultado(salida, out_file)