Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import gcd
- class Fraction:
- def __init__(self, *args):
- if len(args) == 2:
- self.num = args[0]
- self.denom = args[1]
- self._sokr()
- elif len(args) == 1:
- self.num = int(args[0][:args[0].find('/')])
- self.denom = int(args[0][args[0].find('/') + 1:])
- self._sokr()
- def _sokr(self):
- gcd_res = gcd(self.num, self.denom)
- if gcd_res > 1:
- self.num = self.num // gcd_res
- self.denom = self.denom // gcd_res
- def numerator(self, num=None):
- if num is None:
- return self.num
- else:
- self.num = num
- self._sokr()
- def denominator(self, num=None):
- if num is None:
- return self.denom
- else:
- self.denom = num
- self._sokr()
- def __str__(self):
- return f'{self.num}/{self.denom}'
- def __repr__(self):
- return f'Fraction({self.num}, {self.denom})'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement