Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- from typing import *
- class Fraction:
- """Class for functions with fractions"""
- def __init__(self: "Fraction", a: Any = 0, b: int = 1):
- if isinstance(a, str):
- if a.find("/") != -1:
- self.a, self.b = list(map(int, a.split("/")))
- self.reduce()
- elif a.find(" ") != -1:
- self.a, self.b = list(map(int, a.split()))
- self.reduce()
- else:
- self.a = int(a)
- self.b = 1
- self.reduce()
- elif isinstance(a, int) and isinstance(b, int):
- self.a = a
- self.b = b
- self.reduce()
- elif isinstance(a, Fraction):
- self.a = a.a
- self.b = a.b
- self.reduce()
- def __str__(self) -> str:
- if self.b == 1:
- return str(self.a)
- else:
- return str(self.a) + "/" + str(self.b)
- def reduce(self: "Fraction") -> None:
- gcd = math.gcd(self.a, self.b)
- self.a = self.a // gcd
- self.b = self.b // gcd
- if self.b < 0:
- self.a = -self.a
- self.b = -self.b
- def __lt__(self: "Fraction", other: Any) -> bool:
- if isinstance(other, Fraction):
- return(self.a * other.b < other.a * self.b)
- else:
- return(self.a < other * self.b)
- def __le__(self: "Fraction", other: Any) -> bool:
- if isinstance(other, Fraction):
- return(self.a * other.b <= other.a * self.b)
- else:
- return(self.a <= other * self.b)
- def __eq__(self: "Fraction", other: Any) -> bool:
- if isinstance(other, Fraction):
- return(self.a * other.b == other.a * self.b)
- else:
- return(self.a == other * self.b)
- def __ne__(self: "Fraction", other: Any) -> bool:
- if isinstance(other, Fraction):
- return(self.a * other.b != other.a * self.b)
- else:
- return(self.a != other * self.b)
- def __gt__(self: "Fraction", other: Any) -> bool:
- if isinstance(other, Fraction):
- return(self.a * other.b > other.a * self.b)
- else:
- return(self.a > other * self.b)
- def __ge__(self: "Fraction", other: Any) -> bool:
- if isinstance(other, Fraction):
- return(self.a * other.b >= other.a * self.b)
- else:
- return(self.a >= other * self.b)
- def __mul__(self: "Fraction", other: Any) -> Any:
- if isinstance(other, Fraction):
- return(Fraction(self.a * other.a, self.b * other.b))
- elif isinstance(other, int):
- return(Fraction(self.a * other, self.b))
- elif isinstance(other, float):
- return((self.a * other) / self.b)
- def __rmul__(self: "Fraction", other: Any) -> Any:
- if isinstance(other, Fraction):
- return(Fraction(self.a * other.a, self.b * other.b))
- elif isinstance(other, int):
- return(Fraction(self.a * other, self.b))
- elif isinstance(other, float):
- return((self.a * other) / self.b)
- def __imul__(self: "Fraction", other: Any) -> Any:
- if isinstance(other, Fraction):
- self.a *= other.a
- self.b *= other.b
- return(Fraction(self.a, self.b))
- elif isinstance(other, int):
- self.a *= other
- return(Fraction(self.a, self.b))
- elif isinstance(other, float):
- return((self.a * other) / self.b)
- def __truediv__(self: "Fraction", other: Any) -> Any:
- if isinstance(other, Fraction):
- return(Fraction(self.a * other.b, self.b * other.a))
- elif isinstance(other, int):
- return(Fraction(self.a, self.b * other))
- elif isinstance(other, float):
- return(self.a / (self.b * other))
- def __rtruediv__(self: "Fraction", other: Any) -> Any:
- if isinstance(other, Fraction):
- return(Fraction(self.b * other.a, self.a * other.b))
- elif isinstance(other, int):
- return(Fraction(self.b * other, self.a))
- elif isinstance(other, float):
- return((self.b * other) / self.a)
- def __itruediv__(self: "Fraction", other: Any) -> Any:
- if isinstance(other, Fraction):
- new_a = self.a * other.b
- new_b = self.b * other.a
- return(Fraction(new_a, new_b))
- elif isinstance(other, int):
- self.b *= other
- return(Fraction(self.a, self.b))
- elif isinstance(other, float):
- return(self.a / (self.b * other))
- def __add__(self: "Fraction", other: Any) -> Any:
- if isinstance(other, Fraction):
- return(Fraction(self.a * other.b + other.a * self.b, self.b * other.b))
- elif isinstance(other, int):
- return(Fraction(self.a + self.b * other, self.b))
- elif isinstance(other, float):
- return((self.a / self.b) + other)
- def __radd__(self: "Fraction", other: Any) -> Any:
- if isinstance(other, Fraction):
- return(Fraction(self.a * other.b + other.a * self.b, self.b * other.b))
- elif isinstance(other, int):
- return(Fraction(self.a + self.b * other, self.b))
- elif isinstance(other, float):
- return((self.a / self.b) + other)
- def __iadd__(self: "Fraction", other: Any) -> Any:
- if isinstance(other, Fraction):
- new_a = self.a * other.b + other.a * self.b
- new_b = self.b * other.b
- return(Fraction(new_a, new_b))
- elif isinstance(other, int):
- self.a = self.a + self.b * other
- return(Fraction(self.a, self.b))
- elif isinstance(other, float):
- return((self.a / self.b) + other)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement