目前尚不清楚您的输入数据是什么样的,因此我将做出一些假设。
首先,我将假设您每周有一组离散的教学时段 - 比如说,['Monday 9am-10am', 'Monday 10am-11am', ...]
等等,并且每个课程表都包含这些教学时段的一个子集。
其次,我将假设每组时期恰好是一个完整的学期——没有部分学期——并且我们一次只考虑一个学期的课程。
第三,我将假设所有课程都是不同的——你不能让 MATH101 和 ARTMATH101 共用一个房间和一位教授——如果一位老师正在教授一门课程,他就不能(合法地)同时教授另一门课程;换句话说,“一次只能教一节课”的规则没有例外。
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)