-4

我这个功能的主要目的是检查教授是否同时教授两门课程。

def FacultyMemberOnneClass(self):
    for P in ProfessorList:
        for CL in ClassRoomList:
            for CO in CourseList:
                if CO.ProfessorId  == P.ProfessorId: 
                    Start = CO.StartTime() 
                    End = CO.EndTime() 
                    TimeRange = range(Start, End, .25) 
                    TimeRangeSet = Set(TimeRange)
4

2 回答 2

0

我有点不确定您的代码的上下文,但我认为您正在尝试创建一组 python 时间。

您应该好好看看datetime数据类型。下面的代码假定您正在尝试创建一组类型 datetime.time 。我已经在 Python2.6 中对此进行了测试。

import datetime

start = datetime.datetime(2012,1,1,9,0) # 09:00 
end = datetime.datetime(2012,1,1,17) # 17:00
intervalSeconds = (end-start).seconds # seconds between start and end time.
intervalBlocks = intervalSeconds / (60*15) # 15 minute blocks between start and end time.

timeRange = [(start + datetime.timedelta(minutes=15*i)).time()
    for i in range(intervalBlocks+1)]
timeRangeSet = set(timeRange)
于 2012-07-06T05:11:32.400 回答
0

目前尚不清楚您的输入数据是什么样的,因此我将做出一些假设。

首先,我将假设您每周有一组离散的教学时段 - 比如说,['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)
于 2012-07-06T14:56:16.133 回答