0%

Fraction

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Fraction:
def __init__(self, numerator: int, denominator: int):
self.numerator = numerator
self.denominator = denominator

def __str__(self):
return f'{self.numerator} / {self.denominator}'

def __lt__(self, rhs: Fraction):
l, r = self.numerator * rhs.denominator, self.denominator * rhs.numerator
return l < r

def __add__(self, rhs: Fraction):
numerator, denominator = self.numerator * rhs.denominator + rhs.numerator * self.denominator, \
self.denominator * rhs.denominator
gcd_val = math.gcd(numerator, denominator)
return Fraction(numerator // gcd_val, denominator // gcd_val)

def __sub__(self, rhs: Fraction):
numerator, denominator = rhs.numerator, rhs.denominator
return self.__add__(Fraction(-numerator, denominator))