human
I am working on the file "ExercisesSolutions.ipynb".
The first task is:
Exercise 2
Define a class Rational that uses the normal_form function to store the rational number in the appropriate form. Define a __repr__ function that prints a string that looks like $\frac{n}{d}$ (hint: use len(str(number)) to find the number of digits of an integer). Test it on the cases above.
Solution
Can you write Python code for it?
 ... What is a Python code to find the number of digits of an integer)?
assistant
#Here is Python code to find the number of digits of an integer).

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 __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

q1 = Rational(3, 2)
print(q1)
q2 = Rational(15, 3)
print(q2)
q3 = Rational(20, 42)
print(q3)