|
1 | 1 | import numpy as np
|
2 | 2 | import math
|
3 | 3 |
|
| 4 | +filename = 'data/{}.txt'.format('a') |
| 5 | + |
| 6 | +with open(filename) as f_in: |
| 7 | + content = f_in.read().split("\n")[:-1] |
| 8 | + |
| 9 | + |
| 10 | +def eval_output(output_data): |
| 11 | + """ |
| 12 | + ex. output_data |
| 13 | + [ |
| 14 | + [ |
| 15 | + [1, ["a", "b"]] |
| 16 | + ], |
| 17 | + [ |
| 18 | + [2, ["c", "b"]] |
| 19 | + ], |
| 20 | + [ |
| 21 | + [3, ["a", "c"]], |
| 22 | + [4, ["b", "d"]] |
| 23 | + ], |
| 24 | + [ |
| 25 | + [5, ["c", "d"]] |
| 26 | + ], |
| 27 | + [ |
| 28 | + [6, ["a", "c"]], |
| 29 | + [7, ["c", "d"]] |
| 30 | + ] |
| 31 | + ] |
| 32 | + """ |
| 33 | + score = 0 |
| 34 | + prev_slide_tags = [] |
| 35 | + for slide in output_data: |
| 36 | + slide_tags = [] |
| 37 | + for image in slide: |
| 38 | + slide_tags += image[1] |
| 39 | + slide_tags = set(slide_tags) |
| 40 | + common = len(slide_tags.intersection(prev_slide_tags)) |
| 41 | + in_previous_only = len([t for t in prev_slide_tags if t not in slide_tags]) |
| 42 | + in_current_only = len([t for t in slide_tags if t not in prev_slide_tags]) |
| 43 | + minimum = min(common, in_current_only, in_previous_only) |
| 44 | + |
| 45 | + prev_slide_tags = slide_tags |
| 46 | + score += minimum |
| 47 | + # print("Common:", common) |
| 48 | + # print("In previous only:", in_previous_only) |
| 49 | + # print("In current only:", in_current_only) |
| 50 | + # print("Min:", minimum) |
| 51 | + |
| 52 | + print() |
| 53 | + return score |
| 54 | + |
| 55 | + |
| 56 | +# print("Score:", eval_output([[[1, ["a", "b"]]], [[2, ["c", "b"]]], [[3, ["a", "c"]], [4, ["b", "d"]]], [[5, ["c", "d"]]], [[6, ["a", "c"]], [7, ["c", "d"]]]])) |
| 57 | + |
| 58 | + |
| 59 | +def create_output_file(output_data): |
| 60 | + # ex. output_data = [[1], [2], [3, 4], [5], [6, 7]] |
| 61 | + with open(filename.replace('data', 'out'), "w") as f_out: |
| 62 | + f_out.write(str(len(output_data)) + '\n') |
| 63 | + [f_out.write(' '.join([str(x) for x in slide]) + '\n') for slide in output_data] |
| 64 | + |
| 65 | + |
| 66 | +class Image: |
| 67 | + def __init__(self, image_id, orientation, tags): |
| 68 | + self.id = image_id |
| 69 | + self.orientation = orientation |
| 70 | + self.tags = sorted(tags) |
| 71 | + |
| 72 | + def __str__(self): |
| 73 | + return "<Image {}: Orientation: {}, Tags: {}>".format(self.id, self.orientation, self.tags) |
| 74 | + |
| 75 | + |
4 | 76 | if __name__ == '__main__':
|
5 |
| - print("HASHCODE 2019") |
| 77 | + print("HASHCODE 2019!\n") |
| 78 | + |
| 79 | + images = [Image(content.index(c)-1, c.split(" ")[0], c.split(" ")[2:]) for c in content[1:]] |
| 80 | + print(len(images), "images loaded") |
| 81 | + [print(i) for i in images] |
| 82 | + # create_output_file([[1], [2], [3, 4], [5], [6, 7]]) |
0 commit comments