Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import itertools as it
- class MyPolynomial(object):
- def __init__(self, *coefficients):
- self.coefficients = list(coefficients)
- self._priv = None
- def __eq__(self, y):
- return str(self) == str(y)
- def __repr__(self):
- if len(self.coefficients) == 0:
- return '{}({})'.format(
- self.__class__.__name__, '0'
- )
- if sum(self.coefficients) == 0:
- return '{}({})'.format(
- self.__class__.__name__, '0'
- )
- return '{}({})'.format(
- self.__class__.__name__, ', '.join((str(x) for x in self.coefficients))
- )
- def __add__(self, other):
- n = len(self.coefficients)
- p = len(other.coefficients)
- if (n >= p):
- new_coefficients = [self.coefficients[i] for i in range(n)]
- for i in range(p):
- new_coefficients[i] += other.coefficients[i]
- if (p >= n):
- new_coefficients = [other.coefficients[i] for i in range(p)]
- for i in range(n):
- new_coefficients[i] += self.coefficients[i]
- return MyPolynomial(*new_coefficients)
- def __iadd__(self, other):
- for i, x in enumerate(self.coefficients):
- self.coefficients[i] += other.coefficients[i]
- return self
- def __str__(self):
- out = ''
- size = len(self.coefficients)
- for i in range(size):
- if self.coefficients[i] != 0:
- out += ' + %gx^%d' % (self.coefficients[i],i)
- out = out.replace('+ -', '- ')
- out = out.replace('x^0', '1')
- out = out.replace(' 11 ', ' 1 ')
- if out[0:3] == ' + ':
- out = out[3:]
- if out[0:3] == ' - ':
- out = '-' + out[3:]
- if len(out) == 0:
- return '0'
- return out
- def from_iterable(iterables):
- return MyPolynomial(*iterables)
- def degree(self):
- if len(self.coefficients) == 0:
- return 0
- if sum(self.coefficients) == 0:
- return 0
- i = 1
- iterator = len(self.coefficients)-1
- print("iter:"+str(iterator))
- for x in self.coefficients:
- if self.coefficients[iterator] == 0:
- i = i + 1
- else:
- break
- iterator = iterator - 1
- return len(self.coefficients) - i
- def __mul__(self, other):
- if isinstance(other, (float, int)): other = MyPolynomial(other)
- out = []
- for i in range(len(self.coefficients) + len(other.coefficients) - 1):
- out.append(0)
- for i in range(0, len(self.coefficients)):
- for j in range(0, len(other.coefficients)):
- out[i+j] = out[i+j] + self.coefficients[i] * other.coefficients[j]
- return MyPolynomial.from_iterable(out)
- def __rmul__(self, other):
- return self * other
- def __imul__(self, other):
- if isinstance(other, (float, int)): other = MyPolynomial(other)
- temp = self.coefficients
- for i in range(len(temp)):
- self.coefficients[i] = 0
- for i in range(len(other.coefficients)):
- self.coefficients.append(0)
- for i in range(0, len(temp)):
- for j in range(0, len(other.coefficients)):
- self.coefficients[i+j] += temp[i] * other.coefficients[j]
- return self
- assert MyPolynomial(6, 14, 8) == MyPolynomial(2, 2) * MyPolynomial(3, 4)
- assert MyPolynomial(6, 14, 8) == MyPolynomial(3, 7, 4) * 2
- assert MyPolynomial(6, 14, 8) == 2 * MyPolynomial(3, 7, 4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement