Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import pandas as pd
- import csv
- def open_file(path):
- result = []
- with open(path, 'r') as csvfile:
- csvreader = csv.reader(csvfile, delimiter=' ')
- for row in csvreader:
- result.append(row)
- return result
- def write_file(path, result):
- with open(path, 'w', newline='') as csvfile:
- spamwriter = csv.writer(csvfile, delimiter=' ')
- for line in result:
- spamwriter.writerow(line)
- def calc_score(result, info):
- amount_libraries = int(info[0][1])
- days_avail = int(info[0][2])
- book_scores = info[1]
- score = 0
- lib_registered = [None] * amount_libraries
- possible_shippings = []
- i = 2
- while i < len(info):
- possible_shippings.append(int(info[i][2]))
- i += 2
- planned_regs = []
- sent_books = []
- planned_books = [None] * amount_libraries
- registering = False
- reg_countdown = 0
- i = 1
- while i < len(result):
- lib = result[i][0]
- planned_regs.append(lib)
- i += 1
- planned_books[lib] = (result[i])
- i += 1
- for day in range(days_avail):
- #refresh shipping amount for all libraries
- shippings_left = possible_shippings.copy()
- #registering if possible
- if not registering and len(planned_regs) > 0:
- row = 2 + (planned_regs[0]*2)
- reg_countdown = int(info[row][1])
- registering = True
- else:
- reg_countdown -= 1
- if reg_countdown == 0 and len(planned_regs) > 0:
- lib = planned_regs.pop(0)
- lib_registered[lib] = True
- registering = False
- for i in range(len(lib_registered)):
- if lib_registered[i] and shippings_left[i] > 0:
- book_list = planned_books[i]
- for j in range(shippings_left[i]):
- if book_list is not None and len(book_list) > 0:
- book = planned_books[i].pop(0)
- shippings_left[i] -= 1
- if book not in sent_books:
- score += int(book_scores[int(book)])
- sent_books.append(book)
- return score
- def create_lib_arr(info):
- amount_libraries = int(info[0][1])
- book_scores = info[1]
- result = []
- for lib_num in range(amount_libraries):
- lib_line = info[2 + (lib_num*2)]
- book_line = info[3 + (lib_num*2)]
- book_line.sort(key=lambda x:int(book_scores[int(x)]))
- lib_line.append(book_line)
- lib_line.append(lib_num)
- result.append(lib_line)
- return result
- def max_score_in_remaining_days(lib, info, days):
- book_amount = int(lib[0])
- reg_time = int(lib[1])
- ship_volume = int(lib[2])
- books = lib[3]
- book_scores = info[1]
- book_days = days - reg_time
- amount_sendable_books = book_days * ship_volume
- score = 0
- i = 0
- while i < amount_sendable_books and i < book_amount:
- book = books[i]
- score += int(book_scores[int(book)])
- i += 1
- return score
- def create_list_with_maxes(lib_arr, info):
- avail_days = int(example[0][2])
- result = []
- while avail_days > 0:
- best = find_best_in_list(lib_arr, info, avail_days)
- if best is None:
- break
- else:
- result.append(best)
- avail_days -= int(best[1])
- return result
- def find_best_in_list(lib_arr, info, days):
- best_score = 0
- index = -1
- for i in range(len(lib_arr)):
- score = max_score_in_remaining_days(lib_arr[i], info, days)
- if score > best_score:
- best_score = score
- index = i
- if index != -1:
- return lib_arr.pop(index)
- else:
- return None
- def transform_lib_to_result(lib_arr):
- result = []
- result.append([len(lib_arr)])
- for lib in lib_arr:
- result.append([lib[4], len(lib[3])])
- result.append(lib[3])
- return result
- example = open_file('a_example.txt')
- lib_arr = create_lib_arr(example)
- sorted = create_list_with_maxes(lib_arr, example)
- result = transform_lib_to_result(sorted)
- write_file('a.txt', result)
- print(calc_score(result, example))
- example = open_file('b_read_on.txt')
- lib_arr = create_lib_arr(example)
- sorted = create_list_with_maxes(lib_arr, example)
- result = transform_lib_to_result(sorted)
- write_file('b.txt', result)
- print(calc_score(result, example))
- example = open_file('c_incunabula.txt')
- lib_arr = create_lib_arr(example)
- sorted = create_list_with_maxes(lib_arr, example)
- result = transform_lib_to_result(sorted)
- write_file('c.txt', result)
- print(calc_score(result, example))
- example = open_file('d_tough_choices.txt')
- lib_arr = create_lib_arr(example)
- sorted = create_list_with_maxes(lib_arr, example)
- result = transform_lib_to_result(sorted)
- write_file('d.txt', result)
- print(calc_score(result, example))
- example = open_file('e_so_many_books.txt')
- lib_arr = create_lib_arr(example)
- sorted = create_list_with_maxes(lib_arr, example)
- result = transform_lib_to_result(sorted)
- write_file('e.txt', result)
- print(calc_score(result, example))
- example = open_file('f_libraries_of_the_world.txt')
- lib_arr = create_lib_arr(example)
- sorted = create_list_with_maxes(lib_arr, example)
- result = transform_lib_to_result(sorted)
- write_file('f.txt', result)
- print(calc_score(result, example))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement