-
Notifications
You must be signed in to change notification settings - Fork 1
/
photo.py
97 lines (78 loc) · 2.82 KB
/
photo.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
import sys
args = sys.argv
input_file = open(args[1], 'r')
output_file = open(args[2], 'w')
lines = int(input_file.readline())
horizontal_slides = {}
vertical_slides = {}
verticals = {}
transitions_slides = {}
transitions = []
best_transitions = []
tags = []
for i in range(lines):
photo_data = input_file.readline().rstrip()
photo_data_list = photo_data.split(' ')
# separate horizontals
if photo_data_list[0] == 'H':
for j in range(int(photo_data_list[1])):
tags.append(photo_data_list[j + 2])
horizontal_slides.update({(i,): set(tags)})
# separate verticals
if photo_data_list[0] == 'V':
for j in range(int(photo_data_list[1])):
tags.append(photo_data_list[j + 2])
verticals[i] = tags
tags = []
# create slides out of verticals
v_len = len(verticals)
if v_len == 2:
v_ids = tuple(list(verticals.keys()))
v_tags = set(verticals[1] + verticals[2])
vertical_slides.update({v_ids: v_tags})
elif v_len > 2:
verticals1 = list(verticals.items())[:v_len // 2]
verticals2 = list(verticals.items())[v_len // 2:]
for i, j in zip(verticals1, verticals2):
v_ids = (i[0], j[0])
v_tags = set(i[1] + j[1])
vertical_slides.update({v_ids: v_tags})
horizontal_transitions = sorted(
horizontal_slides.items(), key=lambda kv: kv[1])
vertical_transitions = sorted(vertical_slides.items(), key=lambda kv: kv[1])
len_hor = len(horizontal_slides)
len_ver = len(vertical_slides)
if len_hor > 0 and len_ver > 0:
transitions_slides = {**horizontal_slides, **vertical_slides}
elif len_hor == 0:
transitions_slides = vertical_slides
else:
transitions_slides = horizontal_slides
transitions = sorted(transitions_slides.items(), key=lambda kv: kv[1])
# data for output
S = len(transitions) # no. of slides
output_file.write("{}\n".format(S))
# re-arrange slides based off the interest factor
def interest_factor(tags_1, tags_2):
tags_intersection = tags_1.intersection(tags_2)
tags_in_1_not_2 = tags_1.difference(tags_2)
tags_in_2_not_1 = tags_2.difference(tags_1)
return len(min([tags_intersection, tags_in_1_not_2, tags_in_2_not_1]))
# start from thr first slide available
slide = transitions[0]
transitions.pop(transitions.index(slide))
best_transitions.append(slide)
# repeatedly select the next interesting slide
while len(transitions) > 0:
next_slide = max([(interest_factor(slide[1], transitions[i][1]), transitions[i])
for i in range(min(100, len(transitions)))])
best_transitions.append(next_slide[1])
transitions.pop(transitions.index(next_slide[1]))
slide = next_slide[1]
for i in best_transitions:
if len(i[0]) == 2:
output_file.write("{} {}\n".format(i[0][0], i[0][1]))
else:
output_file.write("{}\n".format(i[0][0]))
output_file.close()
input_file.close()