Quiz 3 OOP Practice Answer Key


Videos to explain this code will be released later this week!

Exercise 1 - Instagram

InstaUser

class InstaUser:
                    """Instagram user object."""
                    username: str
                    password: str
                    followers: List[str]
                    following: List[str]
                
                    def __init__(self, username: str, password: str):
                        """Instagram user constructor."""
                        self.username = username
                        self.password = password
                        self.followers = []
                        self.following = []
                
                    def getFollowerCount(self) -> int:
                        """Get user follower count."""
                        return len(self.followers)
                
                    def getFollowingCount(self) -> int:
                        """Get amount of accounts user follows."""
                        return len(self.following)
                
                    def getMutuals(self) -> List[str]:
                        """Get mutual users by username: people that this user follows that also follow this user."""
                        mutuals: List[str] = []
                        for person in self.following:
                            if person in self.followers:
                                mutuals.append(person)
                        return mutuals
                
                    def follow(self, person: InstaUser) -> None:
                        """Follow someone."""
                        self.following.append(person.username)
                        person.addFollower(self)
                
                    def unfollow(self, person: InstaUser) -> None:
                        """Unfollow someone."""
                        if person.username in self.following:
                          index: int = -1
                          for i in range(len(self.following)):
                              if self.following[i] == person.username:
                                  index = i
                          self.following.pop(index)
                        person.removeFollower(self)
                
                    def addFollower(self, person: InstaUser) -> None:
                        """Add a follower to this insta account."""
                        self.followers.append(person.username)
                
                    def removeFollower(self, person: InstaUser) -> None:
                        """Remove a follower from this insta account."""
                        if person.username in self.followers:
                          index: int = -1
                          for i in range(len(self.followers)):
                              if self.followers[i] == person.username:
                                  index = i
                          self.followers.pop(index)
                
                    def changePassword(self, admin: Admin, password: str) -> bool:
                        """Change account password using an admin."""
                        result: bool = admin.changePassword(self, password)
                        return result

Admin

Exercise 2 - Classception

class Student:
                    """Models a student during course registration period."""
                    
                    name: str
                    schedule: Dict[str, str]
                    credit_hours: int
                    full_time: bool
                
                    def __init__(self, name: str, full_time: bool):
                        """Constructor to initialize attributes."""
                        self.name = name
                        self.schedule = {}
                        self.credit_hours = 0
                        self.full_time = full_time
                
                    def add_course(self, course: str, time: str, credit_hours: int) -> None:
                        """Adds a course to the student's schedule if requirements are met."""
                        if self.full_time and self.credit_hours + credit_hours > 18:
                            print("Add unsuccessful: too many credit hours")
                        elif self.full_time and self.credit_hours + credit_hours <= 18:
                            if course in self.schedule:
                                self.schedule[course] = time
                            else:
                                self.schedule[course] = time
                                self.credit_hours += credit_hours
                        else:
                            if self.credit_hours + credit_hours > 8:
                                print("Add unsuccessful: too many credit hours")
                            else:
                                if course in self.schedule:
                                    self.schedule[course] = time
                                else:
                                    self.schedule[course] = time
                                    self.credit_hours += credit_hours
                
                    def drop_course(self, course: str, credit_hours: int) -> None:
                        """Drops a course from the student's schedule if requirements are met."""
                        if self.full_time and self.credit_hours - credit_hours < 12:
                            print("Drop unsuccessful: must take at least 12 credit hours to maintain full time status")
                        elif self.full_time and self.credit_hours - credit_hours >= 12:
                            if course in self.schedule:
                                self.schedule.pop(course)
                                self.credit_hours -= credit_hours
                        else:
                            if self.credit_hours - credit_hours <= 0:
                                print("Drop unsuccessful: must be enrolled in a class to maintain student status")
                            else:
                                if course in self.schedule:
                                    self.schedule.pop(course)
                                    self.credit_hours -= credit_hours
                
                    def is_classmate(self, another_student: Student) -> bool:
                        """Checks if another student has any courses at the same time as this student."""
                        common_classes: List[str] = []
                        for course in self.schedule:
                            if course in another_student.schedule:
                                if self.schedule[course] == another_student.schedule[course]:
                                    common_classes.append(course)
                
                        if len(common_classes) > 0:
                            print(f"Hi {another_student.name}, my name is {self.name}!")
                            print(f"This is a list of classes that I have with you: {common_classes}")
                            return True
                        else:
                            print(f"Hi {another_student.name}, my name is {self.name}!")
                            print("Unfortunately, we have no classes together")
                            return False