Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.77 KB | None | 0 0
  1. import math
  2. from typing import *
  3.  
  4.  
  5. class Fraction:
  6.     """Class for functions with fractions"""
  7.     def __init__(self: "Fraction", a: Any = 0, b: int = 1):
  8.         if isinstance(a, str):
  9.             if a.find("/") != -1:
  10.                 self.a, self.b = list(map(int, a.split("/")))
  11.                 self.reduce()
  12.             elif a.find(" ") != -1:
  13.                 self.a, self.b = list(map(int, a.split()))
  14.                 self.reduce()
  15.             else:
  16.                 self.a = int(a)
  17.                 self.b = 1
  18.                 self.reduce()
  19.         elif isinstance(a, int) and isinstance(b, int):
  20.             self.a = a
  21.             self.b = b
  22.             self.reduce()
  23.         elif isinstance(a, Fraction):
  24.             self.a = a.a
  25.             self.b = a.b
  26.             self.reduce()
  27.  
  28.     def __str__(self) -> str:
  29.         if self.b == 1:
  30.             return str(self.a)
  31.         else:
  32.             return str(self.a) + "/" + str(self.b)
  33.  
  34.     def reduce(self: "Fraction") -> None:
  35.         gcd = math.gcd(self.a, self.b)
  36.         self.a = self.a // gcd
  37.         self.b = self.b // gcd
  38.         if self.b < 0:
  39.             self.a = -self.a
  40.             self.b = -self.b
  41.  
  42.     def __lt__(self: "Fraction", other: Any) -> bool:
  43.         if isinstance(other, Fraction):
  44.             return(self.a * other.b < other.a * self.b)
  45.         else:
  46.             return(self.a < other * self.b)
  47.  
  48.     def __le__(self: "Fraction", other: Any) -> bool:
  49.         if isinstance(other, Fraction):
  50.             return(self.a * other.b <= other.a * self.b)
  51.         else:
  52.             return(self.a <= other * self.b)
  53.  
  54.     def __eq__(self: "Fraction", other: Any) -> bool:
  55.         if isinstance(other, Fraction):
  56.             return(self.a * other.b == other.a * self.b)
  57.         else:
  58.             return(self.a == other * self.b)
  59.  
  60.     def __ne__(self: "Fraction", other: Any) -> bool:
  61.         if isinstance(other, Fraction):
  62.             return(self.a * other.b != other.a * self.b)
  63.         else:
  64.             return(self.a != other * self.b)
  65.  
  66.     def __gt__(self: "Fraction", other: Any) -> bool:
  67.         if isinstance(other, Fraction):
  68.             return(self.a * other.b > other.a * self.b)
  69.         else:
  70.             return(self.a > other * self.b)
  71.  
  72.     def __ge__(self: "Fraction", other: Any) -> bool:
  73.         if isinstance(other, Fraction):
  74.             return(self.a * other.b >= other.a * self.b)
  75.         else:
  76.             return(self.a >= other * self.b)
  77.  
  78.     def __mul__(self: "Fraction", other: Any) -> Any:
  79.         if isinstance(other, Fraction):
  80.             return(Fraction(self.a * other.a, self.b * other.b))
  81.         elif isinstance(other, int):
  82.             return(Fraction(self.a * other, self.b))
  83.         elif isinstance(other, float):
  84.             return((self.a * other) / self.b)
  85.  
  86.     def __rmul__(self: "Fraction", other: Any) -> Any:
  87.         if isinstance(other, Fraction):
  88.             return(Fraction(self.a * other.a, self.b * other.b))
  89.         elif isinstance(other, int):
  90.             return(Fraction(self.a * other, self.b))
  91.         elif isinstance(other, float):
  92.             return((self.a * other) / self.b)
  93.  
  94.     def __imul__(self: "Fraction", other: Any) -> Any:
  95.         if isinstance(other, Fraction):
  96.             self.a *= other.a
  97.             self.b *= other.b
  98.             return(Fraction(self.a, self.b))
  99.         elif isinstance(other, int):
  100.             self.a *= other
  101.             return(Fraction(self.a, self.b))
  102.         elif isinstance(other, float):
  103.             return((self.a * other) / self.b)
  104.  
  105.     def __truediv__(self: "Fraction", other: Any) -> Any:
  106.         if isinstance(other, Fraction):
  107.             return(Fraction(self.a * other.b, self.b * other.a))
  108.         elif isinstance(other, int):
  109.             return(Fraction(self.a, self.b * other))
  110.         elif isinstance(other, float):
  111.             return(self.a / (self.b * other))
  112.  
  113.     def __rtruediv__(self: "Fraction", other: Any) -> Any:
  114.         if isinstance(other, Fraction):
  115.             return(Fraction(self.b * other.a, self.a * other.b))
  116.         elif isinstance(other, int):
  117.             return(Fraction(self.b * other, self.a))
  118.         elif isinstance(other, float):
  119.             return((self.b * other) / self.a)
  120.  
  121.     def __itruediv__(self: "Fraction", other: Any) -> Any:
  122.         if isinstance(other, Fraction):
  123.             new_a = self.a * other.b
  124.             new_b = self.b * other.a
  125.             return(Fraction(new_a, new_b))
  126.         elif isinstance(other, int):
  127.             self.b *= other
  128.             return(Fraction(self.a, self.b))
  129.         elif isinstance(other, float):
  130.             return(self.a / (self.b * other))
  131.  
  132.     def __add__(self: "Fraction", other: Any) -> Any:
  133.         if isinstance(other, Fraction):
  134.             return(Fraction(self.a * other.b + other.a * self.b, self.b * other.b))
  135.         elif isinstance(other, int):
  136.             return(Fraction(self.a + self.b * other, self.b))
  137.         elif isinstance(other, float):
  138.             return((self.a / self.b) + other)
  139.  
  140.     def __radd__(self: "Fraction", other: Any) -> Any:
  141.         if isinstance(other, Fraction):
  142.             return(Fraction(self.a * other.b + other.a * self.b, self.b * other.b))
  143.         elif isinstance(other, int):
  144.             return(Fraction(self.a + self.b * other, self.b))
  145.         elif isinstance(other, float):
  146.             return((self.a / self.b) + other)
  147.  
  148.     def __iadd__(self: "Fraction", other: Any) -> Any:
  149.         if isinstance(other, Fraction):
  150.             new_a = self.a * other.b + other.a * self.b
  151.             new_b = self.b * other.b
  152.             return(Fraction(new_a, new_b))
  153.         elif isinstance(other, int):
  154.             self.a = self.a + self.b * other
  155.             return(Fraction(self.a, self.b))
  156.         elif isinstance(other, float):
  157.             return((self.a / self.b) + other)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement