""" A module for rational numbers. """


class Rational:
    """
    A rational number

    Attributes:
    ===========
    @type num: int
        numerator of rational number
    @type denom: int
        denominator of rational number
    """

    def __init__(self, num, denom=1):
        """
        Create new Rational self with numerator num and
        denominator denom --- denom must not be 0.

        @type self: Rational
        @type num: int
        @type denom: int
        @rtype: None
        """
        self.num, self.denom = int(num), int(denom)

    def __eq__(self, other):
        """
        Return whether Rational self is equivalent to other.

        @type self: Rational
        @type other: Rational | Any
        @rtype: bool

        >>> r1 = Rational(3, 5)
        >>> r2 = Rational(6, 10)
        >>> r3 = Rational(4, 7)
        >>> r1 == r2
        True
        >>> r1.__eq__(r3)
        False
        """
        return (type(self) == type(other) and
                self.num * other.denom == self.denom * other.num)

    def __str__(self):
        """
        Return a user-friendly string representation of
        Rational self.

        @type self: Rational
        @rtype: str

        >>> print(Rational(3, 5))
        3 / 5
        """
        return "{} / {}".format(self.num, self.denom)

    def __mul__(self, other):
        """
        Return the product of Rational self and Rational other.

        @type self: Rational
        @type other: Rational
        @rtype: Rational

        >>> print(Rational(3, 5).__mul__(Rational(4, 7)))
        12 / 35
        """
        return Rational(self.num * other.num, self.denom * other.denom)

    def __add__(self, other):
        """
        Return the sum of Rational self and Rational other.

        @type self: Rational
        @type other: Rational
        @rtype: Rational

        >>> print(Rational(3, 5).__add__(Rational(4, 7)))
        41 / 35
        """
        return Rational(self.num * other.denom +
                        other.num * self.denom,
                        self.denom * other.denom)

    def __lt__(self, other):
        """
        Return whether Rational self is less than other.

        @type self: Rational
        @type other: Rational | Any
        @rtype: bool

        >>> Rational(3, 5).__lt__(Rational(4, 7))
        False
        >>> Rational(3, 5).__lt__(Rational(5, 7))
        True
        """
        return self.num * other.denom < self.denom * other.num

if __name__ == "__main__":
    import doctest
    doctest.testmod()

    # default value will be used for denom
    r = Rational(4)
    print(r)

    r1 = Rational(3, 5)
    r2 = Rational(4, 7)

    # equivalent ways of calling methods
    print(r1 + r2)
    print(r1.__add__(r2))
    print(r1 * r2)
    print(r1.__mul__(r2))
    print(r1 == r2)
    print(r1.__eq__(r2))
    print(r1 < r2)
    print(r1.__lt__(r2))

    rlist = []
    rlist.append(r1)
    rlist.append(r2)

    r3 = Rational(3, 5)
    # does the lookup work if we don't implement __eq__?
    if r3 in rlist:
        print("Found it!")

    r4 = Rational(1, 2)
    rlist.append(r4)
    print(rlist[0], " | ",  rlist[1], " | ", rlist[2])
    # what happens when we try sorting, if we don't implement __lt__?
    rlist.sort()
    print(rlist[0], " | ",  rlist[1], " | ", rlist[2])
