Last active
November 6, 2019 13:27
-
-
Save reevik/a90b9b4bc234ab5f825d0757c09a64df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The meeting matrix can be improved with constraints e.g output has minimum waiting person and | |
# shortest total duration by using constraing programming or linear optimization. | |
# The problem is very similar to N-Queens. | |
# Right now you can find optimum solution by running the code multiple times. | |
import itertools | |
import random | |
from datetime import datetime, timedelta | |
team = ['Erhan', 'Brown', 'Mustermann', 'Jack', 'Joe', 'Rambo', 'Steve'] | |
rooms = ['Flottbek', 'Osterbek', 'Seeve'] | |
pairs = list(itertools.combinations(team, 2)) | |
random.shuffle(pairs) | |
start_time_str = "12.11.2019 10:00" | |
duration_in_mins = 10 | |
meetings = {} | |
coffee=('coffee', 'coffee') | |
def has_capacity(slot, max_size): | |
if len(slot) < max_size: | |
return True | |
return False | |
def fit_into_slot(time_slot, pair): | |
if len(time_slot) > 0: | |
for a, b in time_slot: | |
if a == pair[0] or a == pair[1] or b == pair[0] or b == pair[1]: | |
return False | |
return True | |
def get_person_for_coffee(slot): | |
booked = list(itertools.chain(*slot)) | |
non_booked = [] | |
for person in team: | |
if not person in booked: | |
non_booked.append(person) | |
return non_booked | |
def get_template(rooms, prefix): | |
t = prefix | |
for i in range(len(rooms)): | |
t += '{:20} ' | |
return t | |
def format_pair(pair_list): | |
str_pair_list = [] | |
for i in range(len(rooms)): | |
if i == len(rooms) - 1: | |
str_pair_list.append(','.join(get_person_for_coffee(pair_list))) | |
else: | |
if i < len(pair_list) and isinstance(pair_list[i], tuple): | |
str_pair_list.append(pair_list[i][0] + ":" + pair_list[i][1]) | |
else: | |
str_pair_list.append('-') | |
return get_template(rooms, '').format(*str_pair_list) | |
def process(p_head, p_tail, current_time, mark = None): | |
current_time_str = current_time.strftime("%H:%M") | |
if not current_time_str in meetings: | |
meetings[current_time_str] = [] | |
current_slot = meetings[current_time_str] | |
if not has_capacity(current_slot, len(rooms)): | |
next_time = current_time + timedelta(minutes = duration_in_mins) | |
next_time_str = next_time.strftime("%H:%M") | |
meetings[next_time_str] = [] | |
current_slot = meetings[next_time_str] | |
current_time = next_time | |
if fit_into_slot(current_slot, p_head): | |
current_slot.append(p_head) | |
if len(p_tail) > 0: | |
it = iter(p_tail) | |
process(next(it), list(it), current_time) | |
else: | |
if mark == p_head: | |
current_slot.append("-") | |
if len(p_tail) > 0: | |
process(p_head, p_tail, current_time) | |
if not mark: | |
mark = p_head | |
p_tail.append(p_head) | |
if len(p_tail) > 0: | |
it = iter(p_tail) | |
process(next(it), list(it), current_time, mark) | |
it = iter(pairs) | |
process(next(it), list(it), datetime.strptime(start_time_str, "%d.%m.%Y %H:%M")) | |
if len(rooms) % 2 > 0: | |
rooms.append('coffee') | |
print(get_template(rooms, ' ').format(*rooms)) | |
for key in sorted( meetings.keys() ): | |
print(key + " | " + format_pair(meetings[key])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment