human
I am working on the file "ExercisesSolutions.ipynb".
The first task is:
Exercise 5
Overload the __rmul__ function so that you can multiply a rational by an integer. Check that $\frac{1}{2} \times 2 = 1$ and $\frac{1}{2} + (-1) \times \frac{1}{2} = 0$. Also overload the __sub__ function (using previous functions!) so that you can subtract rational numbers and check that $\frac{1}{2} - \frac{1}{2} = 0$.
Solution
Can you write Python program for it?

Give me a the Python program you requested?
assistant
#Here is the Python program 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 __mul__(a, b):
        
        numerator = a.numerator * b.numerator
        denominator = a.denominator * b.denominator
        return Rational(numerator, denominator)
    
    def __rmul__(self, other):
        
        numerator = self.numerator * other
        return Rational(numerator, self.denominator)
    
    def __sub__(a, b):
        
        return a + (-1)*b
    
    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

half = Rational(1,2)
print(2*half)
print(half+(-1)*half)
print(half-half)