Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. from math import ceil
  2.  
  3. files = ['a_example', 'b_read_on', 'c_incunabula', 'd_tough_choices', 'e_so_many_books', 'f_libraries_of_the_world']
  4.  
  5. def load_data(file):
  6. with open(file, 'r') as f:
  7. res = f.read().split('\n')
  8. if res[-1] == '':
  9. return res[0:-1]
  10. return res
  11.  
  12.  
  13. def parse_data(data):
  14. libraries = []
  15. first_row = data[0].split(' ')
  16. num_of_libs = int(first_row[1])
  17. time = int(first_row[2])
  18. book_scores = [int(elem) for elem in data[1].split(" ")]
  19. for i in range(2, num_of_libs * 2 + 1, 2):
  20. row = data[i].split(' ')
  21. books = [int(book) for book in data[i + 1].split(" ")]
  22. registration_time = int(row[1])
  23. shipping_amount = int(row[2])
  24. num_of_books_send = (time - registration_time) * shipping_amount
  25. books_in_lib = [book_scores[j] for j in books]
  26. books_dict = {k: v for k, v in zip(books, books_in_lib)}
  27. lib = {
  28. 'registration_time': registration_time,
  29. 'shipping_amount': shipping_amount,
  30. 'books': books_dict,
  31. 'max_books': num_of_books_send if num_of_books_send < len(books_in_lib) else len(books_in_lib)
  32. }
  33. lib['books'] = {k: v for k, v in sorted(lib['books'].items(), key=lambda item: item[1], reverse=True)}
  34. libraries.append(lib)
  35.  
  36. return (book_scores, libraries, time)
  37.  
  38.  
  39. def when_books_run_out(library, time_left):
  40. commited_books_amount = sum([1 if v != 0 else 0 for k, v in library['books'].items()])
  41. days_needed = ceil(commited_books_amount / library['shipping_amount'])
  42. res = time_left - days_needed
  43. return res if res > 0 else 0
  44.  
  45. def wasted_days_coefficient(wasted_days, time_left): # time
  46. D = time_left
  47. x = wasted_days
  48. q = 1.3
  49. a = -D*(1-q)*q
  50. p = D - D*(1-q)
  51. return a/(x-p) + q
  52. #(D*(1 - q) * q)/(x - (D - (1 - q) * D)) + q
  53.  
  54. def calculate_coefficients(libs, time_left):
  55. coefficients = []
  56. for lib in libs:
  57. book_values = list(lib['books'].values())
  58. book_score = sum(book_values[0:lib['max_books']])
  59. wasted_days = when_books_run_out(lib, time_left)
  60. wdc = wasted_days_coefficient(wasted_days, time_left)
  61. coefficients.append(book_score / lib['registration_time'] * wdc)
  62. # print(wdc)
  63. return coefficients
  64.  
  65.  
  66. def get_max_coefficient_index(coefficients):
  67. return coefficients.index(max(coefficients))
  68.  
  69.  
  70. def reset_used_books(used_books_indexes):
  71. for i in used_books_indexes:
  72. book_scores[i] = 0
  73.  
  74.  
  75. def recalculate(libs, time_left, book_scores):
  76. for lib in libs:
  77. k = (time_left - lib['registration_time']) * lib['shipping_amount']
  78. lib['max_books'] = k if k < len(lib['books']) else len(lib['books'])
  79. books_indexes = lib['books'].keys()
  80. books_in_lib = [book_scores[j] for j in books_indexes]
  81. books_dict = {k: v for k, v in zip(books_indexes, books_in_lib)}
  82. lib['books'] = books_dict
  83. lib['books'] = {k: v for k, v in sorted(lib['books'].items(), key=lambda item: item[1], reverse=True)}
  84.  
  85.  
  86. def print_result(res):
  87. with open("res.txt", "w+") as f:
  88. f.write(str(len(res)) + "\n")
  89. for k, v in zip(res.keys(), res.values()):
  90. f.write(str(k) + " " + str(len(v)) + "\n")
  91. for book in v:
  92. f.write(str(book))
  93. if book != list(v)[-1]:
  94. f.write(' ')
  95. f.write("\n")
  96.  
  97.  
  98. book_scores, libraries, time = parse_data(load_data(files[2] + ".txt"))
  99. old_time = 0
  100. res = {}
  101. while 0 < time != old_time:
  102. coefficients = calculate_coefficients(libraries, time)
  103. best_library_index = get_max_coefficient_index(coefficients)
  104. best_library = libraries[best_library_index]
  105. print(best_library)
  106. res[best_library_index] = best_library['books'].keys()
  107. reset_used_books(best_library['books'].keys())
  108. old_time = time
  109. time = time - best_library['registration_time']
  110. recalculate(libraries, time, book_scores)
  111.  
  112. print_result(res)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement