Lecture 21 Finger Exercise

The questions below are due on Friday May 08, 2026; 11:59:00 PM.
 
You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.

Question 1. In class, we implemented a class Fraction with multiplication (*) and division (/) operations. Complete the implementation to support addition (+), subtraction (-), and equality testing (==).

Equality testing should consider two Fractions equal if they represent the exact same value. Using the example from lecture code, if b = Fraction(num=-1, denom=4) and c = Fraction(num=3, denom=-12), then b == c should evaluate to True.

You may find the documentation at these links helpful: numeric operations and equality testing.

The initial code below supplies an internal helper method _find_gcd() and proposes another one _simplify(). You are encouraged to complete the latter and use it anywhere in the class definition.

class Fraction:

    def _find_gcd(self):  # greatest common divisor
        if self.num == 0:
            return 1
        for candidate in range(min(abs(self.num), abs(self.denom)), 0, -1):
            if self.num % candidate == 0 and self.denom % candidate == 0:
                return candidate

    def _simplify(self):
        ...

    def __init__(self, num, denom):
        assert isinstance(num, int)
        assert isinstance(denom, int) and denom != 0
        self.num = num
        self.denom = denom

    def __str__(self):
        return f"<Fraction {self.num} / {self.denom}>"

    def __float__(self):
        return self.num / self.denom

    def __mul__(self, other):
        top = self.num * other.num
        bottom = self.denom * other.denom
        return Fraction(top, bottom)

    def __truediv__(self, other):
        return self * Fraction(other.denom, other.num)

Include your entire Fraction class definition in the submission box.

Question 2. You are given an Employee class with the following method signatures and docstrings, but the method implementations are is unavailable to you. Implement a Manager class that subclasses Employee and satisfies the given method docstrings given below.
class Employee:
    """A record of an employee's id number and salary."""

    def __init__(self, num, salary=70_000):
        """Initialize an Employee with their specified number and salary."""

    def __str__(self):
        """Return the str "Employee #N", where N is the employee's number."""

    def get_number(self):
        """Retreive the employee's number."""

    def get_salary(self):
        """Retreive the employee's salary."""

    def set_salary(self, salary):
        """Set the employee salary's to a specified int."""


class Manager(Employee):
    """A record of a manager as an employee and which employees they supervise."""

    def __init__(self, num):
        """
        Initialize a Manager with their specified employee number and a
        salary of 80_000.
        """

    def add_supervisee(self, worker):
        """
        Record a non-Manager Employee as being supervised by self, only
        if self is not already supervising another Employee with the
        same number.
        """

    def get_supervisees(self):
        """
        Return a list containing all Employees who are supervised by
        self, in any order. Mutating this list should not affect self.
        """

    def get_supervisee_numbers(self):
        """
        Return a list, sorted in ascending order, of all the employee
        numbers for all Employees being supervised byself.
        """

For example, suppose e1 = Employee(1), e2 = Employee(2), e3 = Employee(1), and m1 = Manager(3).

  • str(m1) should return "Employee #3"
  • m1.get_salary() should return 80_000
  • After running
    m1.add_supervisee(e2)
    m1.add_supervisee(e1)
    m1.add_supervisee(e3)
    
    • set(m1.get_employees()) should be equal to {e1, e2}.
    • m1.get_employee_numbers() should return [1, 2]

Include the entire Manager class definition in the submission box.