class Course:
def __init__(self, name, classroom, instructor, day, start_time, end_time):
self.name = name
self.classroom = classroom
self.instructor = instructor
self.day = day
self.start_time = start_time
self.end_time = end_time
class Schedule:
def __init__(self):
self.courses = []
program = [["9","10","11","12","13","14","15","16","17"],["9","10","11","12","13","14","15","16","17"],["9","10","11","12","13","14","15","16","17"],["9","10","11","12","13","14","15","16","17"],["9","10","11","12","13","14","15","16","17"]]
def add_course(self, course):
self.courses.append(course)
def print_schedule(self):
days = ["Monday","Tuesday","Wednesday","Thursday","Friday"]
program = [["9","10","11","12","13","14","15","16","17"],["9","10","11","12","13","14","15","16","17"],["9","10","11","12","13","14","15","16","17"],["9","10","11","12","13","14","15","16","17"],["9","10","11","12","13","14","15","16","17"]]
for course in self.courses:
for j in range(course.start_time-9,course.end_time-8):
program[days.index(course.day)][j] += f" {course.name} class from {course.instructor} at {course.classroom}"
for i in range(len(days)):
print(days[i],":")
for k in program[i]:
print(k)
schedule = Schedule()
schedule.add_course(Course("Physics","MED A11","James","Monday",9,11))
schedule.add_course(Course("Logic Design","EEB 4105","Jack","Wednesday",9,10))
schedule.add_course(Course("Logic Design","EEB 4205","Jack","Wednesday",15,17))
schedule.print_schedule()
这里我想创建一个每周的日程表,我想在两个类冲突的时候写一些东西,所以他们的self.day需要是相同的,时间需要相交。
有时候我可以做一些
time = {for i in range(start_time,end_time+1)}
if time1.intersection(time2) != 0:
#...
但我不知道如何达到2个不同的课程元素在同一时间。也将是伟大的,如果你有任何建议,为这个代码。
2条答案
按热度按时间nnsrf1az1#
你可以在
Course
类中添加一个方法来检查它是否与另一个Course
冲突。当你添加一个课程时,让它循环遍历现有的课程,看看它是否与你的Schedule
中的现有课程冲突。h7appiyu2#
根据你的代码,你可以为类Schedule定义一个比较函数,你可以创建一个Schedule对象来保存很多课程,所以如果你想访问Schedule中的一门课程,你需要执行schedule.courses[i],但是我建议你在课程类中添加下面的函数来解决你的问题。