CSES - Courses

Your task is to design a class that supports adding courses and their prerequisite relations, and finding a way to take the courses in an order that satisfies the requisites.

If there are multiple orders, any of them is acceptable.

You may assume that the number of courses is at most 50 and that the methods of the class are called at most 100 times. The name of each course is a string of at most 10 characters.

In a file courseplan.py, implement a class CoursePlan with the following methods:

  • constructor
  • add_course adds a course with the given name
  • add_requisite adds a prerequisite
  • find returns a satisfactory course plan as a list (or None if there is none)
class CoursePlan:
    def __init__(self):
        # TODO

    def add_course(self,course):
        # TODO

    def add_requisite(self,course1,course2):
        # TODO

    def find(self):
        # TODO

if __name__ == "__main__":
    c = CoursePlan()
    c.add_course("Ohpe")
    c.add_course("Ohja")
    c.add_course("Tira")
    c.add_course("Jym")
    c.add_requisite("Ohpe","Ohja")
    c.add_requisite("Ohja","Tira")
    c.add_requisite("Jym","Tira")
    print(c.find()) # [Ohpe,Jym,Ohja,Tira]
    c.add_requisite("Tira","Tira")
    print(c.find()) # None