Advertisement
PeVas

C964 K-P

Dec 9th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.20 KB | None | 0 0
  1. from typing import Any
  2. from math import gcd
  3.  
  4.  
  5. class Fraction:
  6.     def __init__(self, x: Any = 0, y: int = 1):
  7.         if isinstance(x, Fraction):
  8.             self.a = x.a  # type: int
  9.             self.b = x.b  # type: int
  10.         else:
  11.             if isinstance(x, str):
  12.                 if not x.isdigit():
  13.                     x, y = map(int, x.replace(' ', '/').split('/'))
  14.                 else:
  15.                     x = int(x)
  16.             elif isinstance(x, float):
  17.                 x, y = x.as_integer_ratio()
  18.             if y < 0:
  19.                 x = -x
  20.                 y = -y
  21.             self.a = x
  22.             self.b = y
  23.         self.reduce()
  24.  
  25.     def reduce(self) -> None:
  26.         if self.a == 0:
  27.             self.b = 1
  28.             return None
  29.         gcd_ab = gcd(self.a, self.b)
  30.         self.a = self.a // gcd_ab
  31.         self.b = self.b // gcd_ab
  32.  
  33.     def __str__(self) -> str:
  34.         if self.b == 1:
  35.             return str(self.a)
  36.         return str(self.a) + '/' + str(self.b)
  37.  
  38.     def __mul__(self, other: Any) -> Any:
  39.         if isinstance(other, int):
  40.             fr = Fraction(self.a * other, self.b)
  41.             fr.reduce()
  42.             return fr
  43.         elif isinstance(other, float):
  44.             self_ = self.a / self.b
  45.             return self_ * other
  46.         else:
  47.             fr = Fraction(self.a * other.a, self.b * other.b)
  48.             fr.reduce()
  49.             return fr
  50.  
  51.     def __rmul__(self, other: Any) -> 'Fraction':
  52.         return self * other
  53.  
  54.     def __imul__(self, other: Any) -> 'Fraction':
  55.         if isinstance(other, int):
  56.             fr = Fraction(self.a * other, self.b)
  57.             fr.reduce()
  58.             self = fr
  59.             return self
  60.         else:
  61.             fr = Fraction(self.a * other.a, self.b * other.b)
  62.             fr.reduce()
  63.             self = fr
  64.             return self
  65.  
  66.     def __truediv__(self, other: Any) -> Any:
  67.         if isinstance(other, int):
  68.             fr = Fraction(self.a, self.b * other)
  69.             fr.reduce()
  70.             return fr
  71.         elif isinstance(other, float):
  72.             self_ = self.a / self.b
  73.             return self_ / other
  74.         else:
  75.             fr = Fraction(self.a * other.b, self.b * other.a)
  76.             fr.reduce()
  77.             return fr
  78.  
  79.     def __rtruediv__(self, other: Any) -> 'Fraction':
  80.         return other * Fraction(self.b, self.a)
  81.  
  82.     def __itruediv__(self, other: Any) -> 'Fraction':
  83.         if isinstance(other, int):
  84.             fr = Fraction(self.a, self.b * other)
  85.             fr.reduce()
  86.             self = fr
  87.             return self
  88.         elif isinstance(other, float):
  89.             self_ = self.a / self.b
  90.             self_ = self_ / other
  91.             self = Fraction(*self_.as_integer_ratio())
  92.             return self
  93.         else:
  94.             fr = Fraction(self.a * other.b, self.b * other.a)
  95.             fr.reduce()
  96.             self = fr
  97.             return self
  98.  
  99.     def __pow__(self, other: Any) -> Any:
  100.         if isinstance(other, int):
  101.             if other < 0:
  102.                 other = abs(other)
  103.                 return Fraction(self.b ** other, self.a ** other)
  104.             return Fraction(self.a ** other, self.b ** other)
  105.         else:
  106.             self_ = float(self)
  107.             other_ = float(other)
  108.             return self_ ** other_
  109.  
  110.     def __rpow__(self, other: Any) -> float:
  111.         return other ** float(self)
  112.  
  113.     def __ipow__(self, other: Any) -> Any:
  114.         self = self ** other
  115.         return self
  116.  
  117.     def __float__(self) -> float:
  118.         return self.a / self.b
  119.  
  120.     def __add__(self, other: Any) -> Any:
  121.         if isinstance(other, int):
  122.             return Fraction(self.a + other * self.b, self.b)
  123.         if isinstance(other, Fraction):
  124.             return Fraction(self.a * other.b + other.a * self.b, self.b * other.b)
  125.         self_ = float(self)
  126.         return self_ + other
  127.  
  128.     def __radd__(self, other: Any) -> Any:
  129.         return self + other
  130.  
  131.     def __iadd__(self, other: Any) -> Any:
  132.         if isinstance(other, int):
  133.             self = Fraction(self.a + other * self.b, self.b)
  134.             return self
  135.         other_ = Fraction(other)
  136.         self = Fraction(self.a * other_.b + other_.a * self.b, self.b * other_.b)
  137.         return self
  138.  
  139.     def __sub__(self, other: Any) -> Any:
  140.         if isinstance(other, int) or isinstance(other, float):
  141.             return self + -(other)
  142.         else:
  143.             return Fraction(self.a * other.b - other.a * self.b, self.b * other.b)
  144.  
  145.     def __rsub__(self, other: Any) -> Any:
  146.         self_ = Fraction(-self.a, self.b)
  147.         return other + self_
  148.  
  149.     def __isub__(self, other: Any) -> Any:
  150.         if isinstance(other, int) or isinstance(other, float):
  151.             self = self + -(other)
  152.             return self
  153.         else:
  154.             self = self - other
  155.             return self
  156.  
  157.     def __pos__(self) -> 'Fraction':
  158.         return self
  159.  
  160.     def __neg__(self) -> 'Fraction':
  161.         return Fraction(-self.a, self.b)
  162.  
  163.     def __abs__(self) -> 'Fraction':
  164.         return Fraction(abs(self.a), self.b)
  165.  
  166.     def __int__(self) -> int:
  167.         return self.a // self.b
  168.  
  169.     def __round__(self, other: int = 0) -> float:
  170.         return round(float(self), other)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement