我不知道如何为每一个教室建立一套

2024-04-20 10:27:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我的主要目的是检查教授是否同时教授两门课。你知道吗

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)

Tags: inself目的forcldefstartend
2条回答

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

您应该好好看看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)

不清楚你的输入数据是什么样子的,所以我要做一些假设。你知道吗

首先,我假设你每周有一组离散的教学周期,比如说,['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)

相关问题 更多 >