human
I am working on the file "ExercisesSolutions.ipynb".
The first task is:
Exercise 3
Overload the __add__ function so that you can add two rational numbers. Test it on $\frac{1}{2} + \frac{1}{3} + \frac{1}{6} = 1$.
Solution
Can you write Python code for it?
 Write me a the Python code you requested?
assistant
#Here is the Python code you requested.

class Rational(object):
    """
    A rational number.
    """
    
    def __init__(self, numerator, denominator):
        
        n, d = normal_form(numerator, denominator)
        
        self.numerator = n
        self.denominator = d
        
        return None
    
    def __add__(a, b):
        
        numerator = a.numerator * b.denominator + b.numerator * a.denominator
        denominator = a.denominator * b.denominator
        return Rational(numerator, denominator)
    
    def __repr__(self):
        
        max_length = max(len(str(self.numerator)), len(str(self.denominator)))
        
        if self.denominator == 1:
            frac = str(self.numerator)
        else:
            numerator = str(self.numerator)+'\n'
            bar = max_length*'-'+'\n'
            denominator = str(self.denominator)
            frac = numerator+bar+denominator
        
        return frac

print(Rational(1,2) + Rational(1,3) + Rational(1,6))