Advertisement
maxim_shlyahtin

7.2

Nov 21st, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.68 KB | None | 0 0
  1. from math import gcd
  2.  
  3.  
  4. class R:
  5.  
  6.     def __init__(self, m: int, n: int) -> None:
  7.         if n == 0:
  8.             raise ZeroDivisionError('Division by zero')
  9.         else:
  10.             self._num = m
  11.             self._den = n
  12.  
  13.     def __add__(self, other: 'R') -> 'R':
  14.         if not isinstance(other, R):
  15.             raise TypeError
  16.         else:
  17.             nok = (self._den * other._den) // gcd(self._den, other._den)
  18.             tmp = self._num * (nok // self._den) + other._num * (nok // other._den)
  19.             return R(tmp, nok)
  20.  
  21.     def __sub__(self, other: 'R') -> 'R':
  22.         if not isinstance(other, R):
  23.             raise TypeError
  24.         else:
  25.             nok = (self._den * other._den) // gcd(self._den, other._den)
  26.             tmp = self._num * (nok // self._den) - other._num * (nok // other._den)
  27.             return R(tmp, nok)
  28.  
  29.     def get_ten(self) -> str:
  30.         if self._den == 0:
  31.             raise ZeroDivisionError('Impossible')
  32.         else:
  33.             return f'{self._num / self._den}'
  34.  
  35.     def __lt__(self, other: 'R') -> 'R':
  36.         if not isinstance(other, R):
  37.             raise TypeError
  38.         else:
  39.             nok = (self._den * other._den) // gcd(self._den, other._den)
  40.             if self._num * (nok // self._den) < other._num * (nok // other._den):
  41.                 return R(self._num, self._den)
  42.  
  43.     def __gt__(self, other: 'R') -> 'R':
  44.         if not isinstance(other, R):
  45.             raise TypeError
  46.         else:
  47.             nok = (self._den * other._den) // gcd(self._den, other._den)
  48.             if self._num * (nok // self._den) > other._num * (nok // other._den):
  49.                 return R(self._num, self._den)
  50.  
  51.     def __str__(self) -> str:
  52.         return f'{self._num}/{self._den}'
  53.  
  54.  
  55. f, f1, f2, f3, f4, f5 = R(9, 2), R(3, 4), R(5, 6), R(6, 7), R(7, 1), R(4, 5)
  56. b = f + f1
  57. print(b)
  58.  
  59.  
  60. class SetR:
  61.  
  62.     def __init__(self, *args: R) -> None:
  63.         for i in range(len(args)):
  64.             if isinstance(args[i], R):
  65.                 self.list = list(args)
  66.             else:
  67.                 raise TypeError("Type isn't R")
  68.  
  69.     def get_max(self) -> R:
  70.         return max(self.list)
  71.  
  72.     def get_min(self) -> R:
  73.         return min(self.list)
  74.  
  75.     def get_count_max(self, item: R) -> int:
  76.         p = self.list[self.list.index(item) + 1:]
  77.         return len(p)
  78.  
  79.     def get_count_min(self, item: R) -> int:
  80.         g = self.list[:self.list.index(item)]
  81.         return len(g)
  82.  
  83.     def __getitem__(self, item: int) -> R:
  84.         return self.list[item]
  85.  
  86.     def __setitem__(self, key: int, value: R) -> list:
  87.         self.list[key] = value
  88.         return self.list
  89.  
  90.     def __len__(self) -> int:
  91.         return len(self.list)
  92.  
  93.     def __str__(self) -> str:
  94.         l = [f'{self.list[i]}' for i in range(len(self.list))]
  95.         return '[' + ', '.join(l) + ']'
  96.  
  97.  
  98. s = SetR(f, f1, f3, f2, f5, f4)
  99. n = SetR(f1, f2, f3)
  100. print(max(s))
  101.  
  102.  
  103. class Polynomial:
  104.  
  105.     def __init__(self, x: SetR) -> None:
  106.         if isinstance(x, SetR):
  107.             self.x = x
  108.         else:
  109.             raise TypeError("Type isn't SetR")
  110.  
  111.     def __add__(self, other: 'Polynomial') -> 'Polynomial':
  112.         storage = []
  113.         if len(self.x) > len(other.x):
  114.             copy = self.x
  115.         else:
  116.             copy = other.x
  117.         for i in range(-min(len(self.x), len(other.x)), 0):
  118.             storage.append(self.x[i] + other.x[i])
  119.         for i in range(-len(storage), 0):
  120.             copy[i] = storage[i]
  121.         return Polynomial(copy)
  122.  
  123.     def __str__(self) -> str:
  124.         l = [f'({self.x[i]} * x**{len(self.x) - i})' for i in range(len(self.x))]
  125.         return ' + '.join(l)
  126.  
  127.  
  128. x = Polynomial(s)
  129. y = Polynomial(n)
  130. print(x + y)
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement