[Solved] I do not understand how to build sets for each classroom


It is not clear what your input data looks like, so I’m going to make some assumptions.

First I am going to assume that you have a discrete set of teaching periods per week – say, ['Monday 9am-10am', 'Monday 10am-11am', ...] and so on, and that every class schedule consists of a subset of these teaching periods.

Second I will assume that each set of periods runs for exactly one whole term – no partial terms – and that we are considering exactly one term’s courses at a time.

Third I will assume that all classes are distinct – you can’t have MATH101 and ARTMATH101 sharing a room and a professor – if a teacher is teaching one course, he can’t (legally) be teaching another course at the same time; in other words, there are no exceptions to the ‘can only be teaching one class at once’ rule.

class Professor(object):
    def __init__(self, name, id):
        self.name = name
        self.id = id

class Course(object):
    def __init__(self, professor_ids, periods):
        self.professors = set(professor_ids)
        self.periods = periods  # where 'period' is an enum of all teaching periods in a week

from collections import Counter

def is_professor_double_booked(professor, all_courses):
    # find all courses taught by this professor
    pcourses = [course in all_courses if professor.id in course.professor_ids]
    # count how many times each period is booked
    period_bookings = Counter(p for course in pcourses for p in course.periods)
    # return (any period booked twice?)
    return len(period_bookings) and (period_bookings.most_common()[0][1] > 1)

def professors_who_are_double_booked(professors, courses):
    for prof in professors:
        if is_professor_double_booked(prof, courses):
            print(prof.name)

1

solved I do not understand how to build sets for each classroom