Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from typing import Any
- from math import gcd
- class Fraction:
- def __init__(self, x: Any = 0, y: int = 1):
- if isinstance(x, Fraction):
- self.a = x.a # type: int
- self.b = x.b # type: int
- else:
- if isinstance(x, str):
- if not x.isdigit():
- x, y = map(int, x.replace(' ', '/').split('/'))
- else:
- x = int(x)
- elif isinstance(x, float):
- x, y = x.as_integer_ratio()
- if y < 0:
- x = -x
- y = -y
- self.a = x
- self.b = y
- self.reduce()
- def reduce(self) -> None:
- if self.a == 0:
- self.b = 1
- return None
- gcd_ab = gcd(self.a, self.b)
- self.a = self.a // gcd_ab
- self.b = self.b // gcd_ab
- def __str__(self) -> str:
- if self.b == 1:
- return str(self.a)
- return str(self.a) + '/' + str(self.b)
- def __mul__(self, other: Any) -> Any:
- if isinstance(other, int):
- fr = Fraction(self.a * other, self.b)
- fr.reduce()
- return fr
- elif isinstance(other, float):
- self_ = self.a / self.b
- return self_ * other
- else:
- fr = Fraction(self.a * other.a, self.b * other.b)
- fr.reduce()
- return fr
- def __rmul__(self, other: Any) -> 'Fraction':
- return self * other
- def __imul__(self, other: Any) -> 'Fraction':
- if isinstance(other, int):
- fr = Fraction(self.a * other, self.b)
- fr.reduce()
- self = fr
- return self
- else:
- fr = Fraction(self.a * other.a, self.b * other.b)
- fr.reduce()
- self = fr
- return self
- def __truediv__(self, other: Any) -> Any:
- if isinstance(other, int):
- fr = Fraction(self.a, self.b * other)
- fr.reduce()
- return fr
- elif isinstance(other, float):
- self_ = self.a / self.b
- return self_ / other
- else:
- fr = Fraction(self.a * other.b, self.b * other.a)
- fr.reduce()
- return fr
- def __rtruediv__(self, other: Any) -> 'Fraction':
- return other * Fraction(self.b, self.a)
- def __itruediv__(self, other: Any) -> 'Fraction':
- if isinstance(other, int):
- fr = Fraction(self.a, self.b * other)
- fr.reduce()
- self = fr
- return self
- elif isinstance(other, float):
- self_ = self.a / self.b
- self_ = self_ / other
- self = Fraction(*self_.as_integer_ratio())
- return self
- else:
- fr = Fraction(self.a * other.b, self.b * other.a)
- fr.reduce()
- self = fr
- return self
- def __pow__(self, other: Any) -> Any:
- if isinstance(other, int):
- if other < 0:
- other = abs(other)
- return Fraction(self.b ** other, self.a ** other)
- return Fraction(self.a ** other, self.b ** other)
- else:
- self_ = float(self)
- other_ = float(other)
- return self_ ** other_
- def __rpow__(self, other: Any) -> float:
- return other ** float(self)
- def __ipow__(self, other: Any) -> Any:
- self = self ** other
- return self
- def __float__(self) -> float:
- return self.a / self.b
- def __add__(self, other: Any) -> Any:
- if isinstance(other, int):
- return Fraction(self.a + other * self.b, self.b)
- if isinstance(other, Fraction):
- return Fraction(self.a * other.b + other.a * self.b, self.b * other.b)
- self_ = float(self)
- return self_ + other
- def __radd__(self, other: Any) -> Any:
- return self + other
- def __iadd__(self, other: Any) -> Any:
- if isinstance(other, int):
- self = Fraction(self.a + other * self.b, self.b)
- return self
- other_ = Fraction(other)
- self = Fraction(self.a * other_.b + other_.a * self.b, self.b * other_.b)
- return self
- def __sub__(self, other: Any) -> Any:
- if isinstance(other, int) or isinstance(other, float):
- return self + -(other)
- else:
- return Fraction(self.a * other.b - other.a * self.b, self.b * other.b)
- def __rsub__(self, other: Any) -> Any:
- self_ = Fraction(-self.a, self.b)
- return other + self_
- def __isub__(self, other: Any) -> Any:
- if isinstance(other, int) or isinstance(other, float):
- self = self + -(other)
- return self
- else:
- self = self - other
- return self
- def __pos__(self) -> 'Fraction':
- return self
- def __neg__(self) -> 'Fraction':
- return Fraction(-self.a, self.b)
- def __abs__(self) -> 'Fraction':
- return Fraction(abs(self.a), self.b)
- def __int__(self) -> int:
- return self.a // self.b
- def __round__(self, other: int = 0) -> float:
- return round(float(self), other)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement