Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. import numpy as np
  2. import pandas as pd
  3. import csv
  4.  
  5.  
  6. def open_file(path):
  7. result = []
  8. with open(path, 'r') as csvfile:
  9. csvreader = csv.reader(csvfile, delimiter=' ')
  10. for row in csvreader:
  11. result.append(row)
  12.  
  13. return result
  14.  
  15.  
  16. def write_file(path, result):
  17. with open(path, 'w', newline='') as csvfile:
  18. spamwriter = csv.writer(csvfile, delimiter=' ')
  19. for line in result:
  20. spamwriter.writerow(line)
  21.  
  22.  
  23. def calc_score(result, info):
  24. amount_libraries = int(info[0][1])
  25. days_avail = int(info[0][2])
  26. book_scores = info[1]
  27. score = 0
  28.  
  29. lib_registered = [None] * amount_libraries
  30. possible_shippings = []
  31. i = 2
  32. while i < len(info):
  33. possible_shippings.append(int(info[i][2]))
  34. i += 2
  35. planned_regs = []
  36. sent_books = []
  37. planned_books = [None] * amount_libraries
  38. registering = False
  39. reg_countdown = 0
  40.  
  41. i = 1
  42. while i < len(result):
  43. lib = result[i][0]
  44. planned_regs.append(lib)
  45. i += 1
  46. planned_books[lib] = (result[i])
  47. i += 1
  48.  
  49. for day in range(days_avail):
  50. #refresh shipping amount for all libraries
  51. shippings_left = possible_shippings.copy()
  52.  
  53. #registering if possible
  54. if not registering and len(planned_regs) > 0:
  55. row = 2 + (planned_regs[0]*2)
  56. reg_countdown = int(info[row][1])
  57. registering = True
  58. else:
  59. reg_countdown -= 1
  60.  
  61. if reg_countdown == 0 and len(planned_regs) > 0:
  62. lib = planned_regs.pop(0)
  63. lib_registered[lib] = True
  64. registering = False
  65.  
  66. for i in range(len(lib_registered)):
  67. if lib_registered[i] and shippings_left[i] > 0:
  68. book_list = planned_books[i]
  69. for j in range(shippings_left[i]):
  70. if book_list is not None and len(book_list) > 0:
  71. book = planned_books[i].pop(0)
  72. shippings_left[i] -= 1
  73. if book not in sent_books:
  74. score += int(book_scores[int(book)])
  75. sent_books.append(book)
  76.  
  77. return score
  78.  
  79.  
  80. def create_lib_arr(info):
  81. amount_libraries = int(info[0][1])
  82. book_scores = info[1]
  83.  
  84. result = []
  85. for lib_num in range(amount_libraries):
  86. lib_line = info[2 + (lib_num*2)]
  87. book_line = info[3 + (lib_num*2)]
  88. book_line.sort(key=lambda x:int(book_scores[int(x)]))
  89. lib_line.append(book_line)
  90. lib_line.append(lib_num)
  91. result.append(lib_line)
  92.  
  93. return result
  94.  
  95.  
  96. def max_score_in_remaining_days(lib, info, days):
  97. book_amount = int(lib[0])
  98. reg_time = int(lib[1])
  99. ship_volume = int(lib[2])
  100. books = lib[3]
  101. book_scores = info[1]
  102. book_days = days - reg_time
  103. amount_sendable_books = book_days * ship_volume
  104. score = 0
  105.  
  106. i = 0
  107. while i < amount_sendable_books and i < book_amount:
  108. book = books[i]
  109. score += int(book_scores[int(book)])
  110. i += 1
  111.  
  112. return score
  113.  
  114.  
  115. def create_list_with_maxes(lib_arr, info):
  116. avail_days = int(example[0][2])
  117. result = []
  118.  
  119. while avail_days > 0:
  120. best = find_best_in_list(lib_arr, info, avail_days)
  121. if best is None:
  122. break
  123. else:
  124. result.append(best)
  125. avail_days -= int(best[1])
  126.  
  127. return result
  128.  
  129.  
  130. def find_best_in_list(lib_arr, info, days):
  131. best_score = 0
  132. index = -1
  133.  
  134. for i in range(len(lib_arr)):
  135. score = max_score_in_remaining_days(lib_arr[i], info, days)
  136. if score > best_score:
  137. best_score = score
  138. index = i
  139.  
  140. if index != -1:
  141. return lib_arr.pop(index)
  142. else:
  143. return None
  144.  
  145.  
  146. def transform_lib_to_result(lib_arr):
  147. result = []
  148. result.append([len(lib_arr)])
  149. for lib in lib_arr:
  150. result.append([lib[4], len(lib[3])])
  151. result.append(lib[3])
  152.  
  153. return result
  154.  
  155.  
  156. example = open_file('a_example.txt')
  157. lib_arr = create_lib_arr(example)
  158. sorted = create_list_with_maxes(lib_arr, example)
  159. result = transform_lib_to_result(sorted)
  160. write_file('a.txt', result)
  161. print(calc_score(result, example))
  162.  
  163. example = open_file('b_read_on.txt')
  164. lib_arr = create_lib_arr(example)
  165. sorted = create_list_with_maxes(lib_arr, example)
  166. result = transform_lib_to_result(sorted)
  167. write_file('b.txt', result)
  168. print(calc_score(result, example))
  169.  
  170. example = open_file('c_incunabula.txt')
  171. lib_arr = create_lib_arr(example)
  172. sorted = create_list_with_maxes(lib_arr, example)
  173. result = transform_lib_to_result(sorted)
  174. write_file('c.txt', result)
  175. print(calc_score(result, example))
  176.  
  177. example = open_file('d_tough_choices.txt')
  178. lib_arr = create_lib_arr(example)
  179. sorted = create_list_with_maxes(lib_arr, example)
  180. result = transform_lib_to_result(sorted)
  181. write_file('d.txt', result)
  182. print(calc_score(result, example))
  183.  
  184. example = open_file('e_so_many_books.txt')
  185. lib_arr = create_lib_arr(example)
  186. sorted = create_list_with_maxes(lib_arr, example)
  187. result = transform_lib_to_result(sorted)
  188. write_file('e.txt', result)
  189. print(calc_score(result, example))
  190.  
  191. example = open_file('f_libraries_of_the_world.txt')
  192. lib_arr = create_lib_arr(example)
  193. sorted = create_list_with_maxes(lib_arr, example)
  194. result = transform_lib_to_result(sorted)
  195. write_file('f.txt', result)
  196. print(calc_score(result, example))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement