Advertisement
kompilainenn

Untitled

Dec 20th, 2023
750
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. from math import gcd
  2.  
  3.  
  4. class Fraction:
  5.     def __init__(self, *args):
  6.         if len(args) == 2:
  7.             self.num = args[0]
  8.             self.denom = args[1]
  9.             self._sokr()
  10.         elif len(args) == 1:
  11.             self.num = int(args[0][:args[0].find('/')])
  12.             self.denom = int(args[0][args[0].find('/') + 1:])
  13.             self._sokr()
  14.  
  15.     def _sokr(self):
  16.         gcd_res = gcd(self.num, self.denom)
  17.         if gcd_res > 1:
  18.             self.num = self.num // gcd_res
  19.             self.denom = self.denom // gcd_res
  20.  
  21.     def numerator(self, num=None):
  22.         if num is None:
  23.             return self.num
  24.         else:
  25.             self.num = num
  26.             self._sokr()
  27.  
  28.     def denominator(self, num=None):
  29.         if num is None:
  30.             return self.denom
  31.         else:
  32.             self.denom = num
  33.             self._sokr()
  34.  
  35.     def __str__(self):
  36.         return f'{self.num}/{self.denom}'
  37.  
  38.     def __repr__(self):
  39.         return f'Fraction({self.num}, {self.denom})'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement