Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_Polynomial(self):
assert str(Polynomial([1]) + Polynomial([1])) == '2'
assert str(Polynomial([3]) + Polynomial([0, 4]) +
Polynomial([-24]) + Polynomial([0, -2])) == '2*x-21'
assert str(Polynomial([-1, 1]) * Polynomial([-1, 1])) == 'x**2-2*x+1'
assert str(-Polynomial([-1, 1])) == '-x+1'
assert str(-Polynomial([1])) == '-1'
assert str(Polynomial([-2, 2]) * Polynomial([2, 2])) == '4*x**2-4'
assert str(Polynomial([-2, 1]) * Polynomial([2, 1])
* Polynomial([2, 1])) == 'x**3+2*x**2-4*x-8'
assert str(Polynomial([2]) * Polynomial([3, 2]) -
Polynomial([0, 1]) + Polynomial([0, 0, 0, 0, 1])) == 'x**4+3*x+6'
assert str(Polynomial([0, 1])) == 'x'
assert str(Polynomial([0, 1])-Polynomial([0, 1])) == '0'
def __add__(self, other):
if len(self.plist) > len(other.plist):
new = [i for i in self.plist]
for i in range(len(other.plist)):
new[i] += other.plist[i]
else:
new = [i for i in other.plist]
for i in range(len(self.plist)):
new[i] += self.plist[i]
return Polynomial(new)
def __mul__(self, other):
result = []
for i in range(len(other.plist)):
result.append(
Polynomial([0] * i + [j * other.plist[i] for j in self.plist]))
return functools.reduce(operator.add, result)
for i in range(len(symbols) - 1):
try:
int(symbols[i])
int(symbols[i + 1])
symbols = symbols[
:i] + [symbols[i] + symbols[i + 1]] + symbols[i + 2:]
changed = True
break
except ValueError:
pass
# convert numbers and x
for i in range(len(symbols)):
try:
int(symbols[i])
symbols[i] = Polynomial([int(symbols[i])])
except ValueError:
if symbols[i] == 'x':
symbols[i] = Polynomial([0, 1])
return symbols
def __neg__(self):
return Polynomial([-1]) * self