Advertisement
Guest User

Python zad 2

a guest
Dec 9th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.51 KB | None | 0 0
  1. from itertools import zip_longest
  2.  
  3. class MyPolynomial:
  4.     def __init__(self, *args):
  5.         if not args:
  6.             args = [0]
  7.  
  8.         self.__coefs = self._truncate_zeros(*args)
  9.  
  10.     def _truncate_zeros(self, *args):
  11.         i = len(args)
  12.  
  13.         while i > 1 and args[i - 1] == 0:
  14.             i -= 1
  15.  
  16.         return list(args[:i])
  17.  
  18.     @classmethod
  19.     def from_iterable(cls, iterable):
  20.         return cls(*iterable)
  21.  
  22.     def __repr__(self):
  23.         return f"{self.__class__.__name__}({', '.join(repr(x) for x in self)})"
  24.  
  25.     def __str__(self):
  26.         st = ""
  27.         for exp, coef in enumerate(self):
  28.             if coef == 0:
  29.               continue
  30.             if exp == 0:
  31.               st += repr(coef)
  32.               continue
  33.             if coef > 0:
  34.               if coef == 1:
  35.                 st += f" + x^{exp}"
  36.               else:
  37.                 st += f" + {coef}x^{exp}"
  38.             if coef < 0:
  39.               if coef == -1:
  40.                 st += f" - x^{exp}"
  41.               else:
  42.                 st += f" - {abs(coef)}x^{exp}"
  43.         return st
  44.  
  45.     def __getitem__(self, idx):
  46.         return self.__coefs[idx]
  47.  
  48.     def __len__(self):
  49.         return len(self.__coefs)
  50.  
  51.     def __eq__(self, other):
  52.         if len(self) != len(other):
  53.             if len(other) == 0 and self[0] == 0:
  54.                 return True
  55.             return False
  56.         return all(i == j for i, j in zip(self, other))
  57.  
  58.     def __call__(self, x):
  59.         return sum([x ** exp * coef for exp, coef in enumerate(self)])
  60.  
  61.     def __add__(self, other):
  62.       if type(other) != type(self):
  63.         res = self.__coefs
  64.         res[0] += other
  65.         return MyPolynomial(*res)
  66.  
  67.       res = [sum(t) for t in zip_longest(self, other, fillvalue=0)]
  68.       return MyPolynomial(*res)
  69.  
  70.     def __iadd__(self, other):
  71.       if type(other) != type(self):
  72.         self.__coefs[0] += other
  73.         return self
  74.      
  75.       res = [sum(t) for t in zip_longest(self, other, fillvalue=0)]
  76.       self.__coefs = self._truncate_zeros(*res)
  77.       return self
  78.  
  79.     def __radd__(self, other):
  80.         return self + other
  81.        
  82.        
  83.     def __sub__(self, other):
  84.       if type(other) != type(self):
  85.         new_coefs = self.__coefs
  86.         new_coefs[0] -= other
  87.         return MyPolynomial(*new_coefs)
  88.       new_coefs = [t - f for (t, f) in zip_longest(self, other, fillvalue=0)]
  89.       return MyPolynomial(*new_coefs)
  90.      
  91.     def __isub__(self, other):
  92.       if type(other) != type(self):
  93.         self.__coefs[0] -= other
  94.         return self
  95.       self.__coefs = [t - f for (t, f) in zip_longest(self, other, fillvalue=0)]
  96.       return self
  97.      
  98.     def __rsub__(self, other):
  99.       new_coefs = [-x for x in self.__coefs]
  100.       new_coefs[0] += other
  101.       return MyPolynomial(*new_coefs)
  102.      
  103.     def __neg__( self ):
  104.       new_coefs = [-x for x in self.__coefs]
  105.       return MyPolynomial(*new_coefs)
  106.      
  107.     def _inner_mul(self, other):
  108.         new_len = len(self) + len(other) - 1
  109.         new_coefs = [0] * new_len
  110.         computaion_matrix = [[0] * len(self) for _ in range(len(other))]
  111.  
  112.         for o_i, o in enumerate(other):
  113.             for s_i, s in enumerate(self):
  114.                 computaion_matrix[o_i][s_i] = o * s
  115.  
  116.         for o_i in range(len(other)):
  117.             for s_i in range(len(self)):
  118.                 new_coefs[o_i + s_i] += computaion_matrix[o_i][s_i]
  119.  
  120.         coefs = self._truncate_zeros(*new_coefs)
  121.         print(coefs)
  122.         return coefs
  123.  
  124.     def __mul__(self, other):
  125.         if type(other) != type(self):
  126.             new_coefs = [x * other for x in self.__coefs]
  127.             return MyPolynomial(*new_coefs)
  128.  
  129.         new_coefs = self._inner_mul(other)
  130.         return MyPolynomial(*new_coefs)
  131.  
  132.     def __imul__(self, other):
  133.         if type(other) != type(self):
  134.             new_coefs = [x * other for x in self.__coefs]
  135.             self.__coefs  = self._truncate_zeros(*new_coefs)
  136.             return self
  137.  
  138.         self.__coefs = self._inner_mul(other)
  139.         return self
  140.  
  141.     def __rmul__(self, other):
  142.         return self * other
  143.  
  144.     def degree(self):
  145.         return len(self) - 1
  146.        
  147.     def __truediv__(self, other):
  148.       if type(other) == int:
  149.         new_coefs = [x / other for x in self.__coefs]
  150.         return MyPolynomial(*new_coefs)
  151.        
  152.       return 0
  153.        
  154.     def __idiv__(self, other):
  155.       self.__coefs = [x / other for x in self.__coefs]
  156.       return self
  157.        
  158. assert MyPolynomial(3, 7, 4) == MyPolynomial(6, 14, 8) / 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement