class Mentor():
    def __init__(self, aName):
        self._students = None
        self._name = None
        self._name = aName
        self._students = []

    def setName(self, aName):
        wasSet = False
        self._name = aName
        wasSet = True
        return wasSet

    def getName(self):
        return self._name

    def getStudent(self, index):
        aStudent = self._students[index]
        return aStudent

    def getStudents(self):
        newStudents = tuple(self._students)
        return newStudents

    def numberOfStudents(self):
        number = len(self._students)
        return number

    def hasStudents(self):
        has = len(self._students) > 0
        return has

    def indexOfStudent(self, aStudent):
        index = (-1 if not aStudent in self._students else self._students.index(aStudent))
        return index

    def isNumberOfStudentsValid(self):
        isValid = self.numberOfStudents() >= Mentor.minimumNumberOfStudents()
        return isValid

    @staticmethod
    def minimumNumberOfStudents():
        return 3

    def addStudent(self, aStudent):
        wasAdded = False
        if (aStudent) in self._students :
            return False
        self._students.append(aStudent)
        if aStudent.indexOfMentor(self) != -1 :
            wasAdded = True
        else :
            wasAdded = aStudent.addMentor(self)
            if not wasAdded :
                self._students.remove(aStudent)
        return wasAdded

    def removeStudent(self, aStudent):
        wasRemoved = False
        if not (aStudent) in self._students :
            return wasRemoved
        if self.numberOfStudents() <= Mentor.minimumNumberOfStudents() :
            return wasRemoved
        oldIndex = (-1 if not aStudent in self._students else self._students.index(aStudent))
        self._students.remove(oldIndex)
        if aStudent.indexOfMentor(self) == -1 :
            wasRemoved = True
        else :
            wasRemoved = aStudent.removeMentor(self)
            if not wasRemoved :
                self._students.insert(oldIndex, aStudent)
        return wasRemoved

    def setStudents(self, *newStudents):
        newStudents = list(newStudents)
        wasSet = False
        verifiedStudents = []
        for aStudent in newStudents:
            if (aStudent) in verifiedStudents :
                continue
            verifiedStudents.append(aStudent)

        if len(verifiedStudents) != len(newStudents) or len(verifiedStudents) < Mentor.minimumNumberOfStudents() :
            return wasSet
        oldStudents = self._students.copy()
        self._students.clear()
        for aNewStudent in verifiedStudents:
            self._students.append(aNewStudent)
            if (aNewStudent) in oldStudents :
                oldStudents.remove(aNewStudent)
            else :
                aNewStudent.addMentor(self)

        for anOldStudent in oldStudents:
            anOldStudent.removeMentor(self)

        wasSet = True
        return wasSet

    def addStudentAt(self, aStudent, index):
        wasAdded = False
        if self.addStudent(aStudent) :
            if index < 0 :
                index = 0
            if index > self.numberOfStudents() :
                index = self.numberOfStudents() - 1
            self._students.remove(aStudent)
            self._students.insert(index, aStudent)
            wasAdded = True
        return wasAdded

    def addOrMoveStudentAt(self, aStudent, index):
        wasAdded = False
        if (aStudent) in self._students :
            if index < 0 :
                index = 0
            if index > self.numberOfStudents() :
                index = self.numberOfStudents() - 1
            self._students.remove(aStudent)
            self._students.insert(index, aStudent)
            wasAdded = True
        else :
            wasAdded = self.addStudentAt(aStudent, index)
        return wasAdded

    def delete(self):
        from Student import Student
        copyOfStudents = self._students.copy()
        self._students.clear()
        for aStudent in copyOfStudents:
            if aStudent.numberOfMentors() <= Student.minimumNumberOfMentors() :
                aStudent.delete()
            else :
                aStudent.removeMentor(self)

    def __str__(self):
        return str(super().__str__()) + "[" + "name" + ":" + str(self.getName()) + "]"


class Student():
    def __init__(self, aNumber):
        self._mentors = None
        self._number = None
        self._number = aNumber
        self._mentors = []

    def setNumber(self, aNumber):
        wasSet = False
        self._number = aNumber
        wasSet = True
        return wasSet

    def getNumber(self):
        return self._number

    def getMentor(self, index):
        aMentor = self._mentors[index]
        return aMentor

    def getMentors(self):
        newMentors = tuple(self._mentors)
        return newMentors

    def numberOfMentors(self):
        self._number = len(self._mentors)
        return self._number

    def hasMentors(self):
        has = len(self._mentors) > 0
        return has

    def indexOfMentor(self, aMentor):
        index = (-1 if not aMentor in self._mentors else self._mentors.index(aMentor))
        return index

    def isNumberOfMentorsValid(self):
        isValid = self.numberOfMentors() >= Student.minimumNumberOfMentors()
        return isValid

    @staticmethod
    def minimumNumberOfMentors():
        return 1

    def addMentor(self, aMentor):
        wasAdded = False
        if (aMentor) in self._mentors :
            return False
        self._mentors.append(aMentor)
        if aMentor.indexOfStudent(self) != -1 :
            wasAdded = True
        else :
            wasAdded = aMentor.addStudent(self)
            if not wasAdded :
                self._mentors.remove(aMentor)
        return wasAdded

    def removeMentor(self, aMentor):
        wasRemoved = False
        if not (aMentor) in self._mentors :
            return wasRemoved
        if self.numberOfMentors() <= Student.minimumNumberOfMentors() :
            return wasRemoved
        oldIndex = (-1 if not aMentor in self._mentors else self._mentors.index(aMentor))
        self._mentors.remove(oldIndex)
        if aMentor.indexOfStudent(self) == -1 :
            wasRemoved = True
        else :
            wasRemoved = aMentor.removeStudent(self)
            if not wasRemoved :
                self._mentors.insert(oldIndex, aMentor)
        return wasRemoved

    def setMentors(self, *newMentors):
        newMentors = list(newMentors)
        wasSet = False
        verifiedMentors = []
        for aMentor in newMentors:
            if (aMentor) in verifiedMentors :
                continue
            verifiedMentors.append(aMentor)

        if len(verifiedMentors) != len(newMentors) or len(verifiedMentors) < Student.minimumNumberOfMentors() :
            return wasSet
        oldMentors = self._mentors.copy()
        self._mentors.clear()
        for aNewMentor in verifiedMentors:
            self._mentors.append(aNewMentor)
            if (aNewMentor) in oldMentors :
                oldMentors.remove(aNewMentor)
            else :
                aNewMentor.addStudent(self)

        for anOldMentor in oldMentors:
            anOldMentor.removeStudent(self)

        wasSet = True
        return wasSet

    def addMentorAt(self, aMentor, index):
        wasAdded = False
        if self.addMentor(aMentor) :
            if index < 0 :
                index = 0
            if index > self.numberOfMentors() :
                index = self.numberOfMentors() - 1
            self._mentors.remove(aMentor)
            self._mentors.insert(index, aMentor)
            wasAdded = True
        return wasAdded

    def addOrMoveMentorAt(self, aMentor, index):
        wasAdded = False
        if (aMentor) in self._mentors :
            if index < 0 :
                index = 0
            if index > self.numberOfMentors() :
                index = self.numberOfMentors() - 1
            self._mentors.remove(aMentor)
            self._mentors.insert(index, aMentor)
            wasAdded = True
        else :
            wasAdded = self.addMentorAt(aMentor, index)
        return wasAdded

    def delete(self):
        from Mentor import Mentor
        copyOfMentors = self._mentors.copy()
        self._mentors.clear()
        for aMentor in copyOfMentors:
            if aMentor.numberOfStudents() <= Mentor.minimumNumberOfStudents() :
                aMentor.delete()
            else :
                aMentor.removeStudent(self)

    def __str__(self):
        return str(super().__str__()) + "[" + "number" + ":" + str(self.getNumber()) + "]"