Home > Mobile >  How to check if 2 elements are same in python OOP?
How to check if 2 elements are same in python OOP?

Time:12-15

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()

Here I wanted to create an weekly schedule, I want it to write something when two classes collide. So their self.day need to be same and the times need to intersect.

For times I can do something like

time = {for i in range(start_time,end_time 1)}
if time1.intersection(time2) != 0:
     #...

But I don't know how to reach 2 different Course elements at the same time. Also would be great if you have any suggestions for this code.

CodePudding user response:

To check if two elements are the same in Python, you can use the == operator. For example, to check if two Course objects have the same day attribute, you can use the following code:

if course1.day == course2.day:
    # courses have the same day

To check if the times for two Course objects intersect, you can use the intersection method of the set class. This method returns a new set that contains the elements that are common to both sets. If the resulting set is empty (i.e., it contains no elements), then the sets do not have any elements in common and therefore do not intersect.

For example, to check if the times for two Course objects intersect, you can use the following code:

time1 = set(range(course1.start_time, course1.end_time   1))
time2 = set(range(course2.start_time, course2.end_time   1))

if time1.intersection(time2):
    # times intersect

To check if two Course objects have the same day and intersecting times, you can combine these two checks, like this:

time1 = set(range(course1.start_time, course1.end_time   1))
time2 = set(range(course2.start_time, course2.end_time   1))

if course1.day == course2.day and time1.intersection(time2):
    # courses have the same day and intersecting times

You can use this code to compare two Course objects and determine if they have the same day and intersecting times. You can then use this information to take appropriate action, such as writing a message indicating that the classes collide.

As for suggestions for your code, one thing you might consider is using a dict to represent the weekly schedule instead of a nested list. This would allow you to use the day of the week as the key for each element in the dict, which would make it easier to add and retrieve courses for a particular day. For example:

class Schedule:
    def __init__(self):
        self.courses = []
        self.program = {
            "Monday": [],
            "Tuesday": [],
            "Wednesday": [],
            "Thursday": [],
            "Friday": [],
        }

    def add_course(self, course):
        self.courses.append(course)
        self.program[course.day].append(course)

    def print_schedule(self):
        for day, courses in self.program.items():
            print(day, ":")
            for course in courses:
                print(f"{course.start_time}-{course.end_time}: {course.name} class from {course.instructor} at {course.classroom}")

With this approach, you can use the program dict to store the courses for each day of the week, and then iterate over the courses for a particular day to print the schedule for that day. You can then use the code from earlier to check if two courses have the same day and intersecting times, and take appropriate action if they do.

CodePudding user response:

So based on your Code, what you can do is define a compare function for the class Schedule, you make a Schedule object to hold many courses. So if you want to access a course within a Schedule you need to do schedule.courses[i]. But I suggest you add the following function to the courses class to solve your problem.

def compare_course(self, other_course):
my_time = {i for i in range(start_time, end_time 1)}
other_time = {i for i in range(other_course.start_time, other_course.end_time 1)}
if my_time.intersection(other_course.time) != set():
    return "Course Collides"
else:
    return "Course does not collide"
  • Related