-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplacer.py
125 lines (83 loc) · 3.04 KB
/
placer.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from tabulate import tabulate
def lowest_intersection(s1, s2):
s1 = sorted(s1)
s2 = sorted(s2)
largest = s1
smallest = s2
if len(s2) > len(s1):
largest = s2
smallest = s1
start = 0
end = len(largest)
while start < end:
if largest[start] in smallest:
return largest[start]
start += 1
return None
TEAMS = ["T{}".format(i) for i in range(1, 5)]
MENTORS = ["M{}".format(i) for i in range(1, 6)]
SLOTS = ["S{}".format(i) for i in range(1, 5)]
INPUT = [("T1", ["M1", "M2", "M3"]),
("T2", ["M1", "M2", "M3", "M4"]),
("T3", ["M2", "M3", "M4"])]
chosen_mentors = []
teams_with_choice = []
mentors_to_teams = {}
for team, mentors in INPUT:
if team not in teams_with_choice:
teams_with_choice.append(team)
for mentor in mentors:
if mentor not in mentors_to_teams:
mentors_to_teams[mentor] = [team]
else:
mentors_to_teams[mentor].append(team)
if mentor not in chosen_mentors:
chosen_mentors.append(mentor)
def attempt_placing(teams, mentors, slots, mentors_to_teams):
mentor_slots_table = {mentor: slots[:] for mentor in mentors}
team_slots_table = {team: slots[:] for team in teams}
result = {}
leftovers = []
for mentor in chosen_mentors:
teams = mentors_to_teams[mentor]
for team in teams:
mentor_free_slots = mentor_slots_table[mentor]
team_free_slots = team_slots_table[team]
first_free_slot = lowest_intersection(mentor_free_slots, team_free_slots)
if first_free_slot is None:
leftovers.append((team, mentor))
continue
if mentor not in result:
result[mentor] = {}
result[mentor][first_free_slot] = team
if len(mentor_free_slots) != 0:
mentor_free_slots.remove(first_free_slot)
if len(team_free_slots) != 0:
team_free_slots.remove(first_free_slot)
return {
"placed": result,
"leftovers": leftovers
}
def build_table_from_result(result, chosen_mentors):
headers = ["Slots"] + chosen_mentors
table = []
for slot in SLOTS:
teams_for_slot = []
for mentor in chosen_mentors:
if slot in result[mentor]:
teams_for_slot.append(result[mentor][slot])
else:
teams_for_slot.append("EMPTY")
table.append([slot] + teams_for_slot)
return tabulate(table, headers=headers, tablefmt="fancy_grid")
placing = attempt_placing(teams=teams_with_choice,
mentors=chosen_mentors,
slots=SLOTS,
mentors_to_teams=mentors_to_teams)
table = build_table_from_result(placing["placed"], chosen_mentors)
print("Leftovers: ")
print(placing["leftovers"])
with open("mentors.html", "w") as f:
f.write("<pre>")
f.write(table)
f.write("</pre>")