class Polynomial: def __init__(self, coefficients): self.coeff = coefficients def __call__(self, x): """Evaluate the polynomial.""" s=0 for i in range(len(self.coeff)): s += self.coeff[i]*x**i return s def __add__(self, other): """Return self + other as Polynomial object.""" if len(self.coeff) > len(other.coeff): result_coeff = self.coeff[:] # copy! for i in range(len(other.coeff)): result_coeff[i] += other.coeff[i] else: result_coeff = other.coeff[:] # copy! for i in range(len(self.coeff)): result_coeff[i] += self.coeff[i] return Polynomial(result_coeff) def __sub__(self, other): neg_coeff = [-coeff for coeff in other.coeff] neg_p = Polynomial(neg_coeff) return self + neg_p def test_Polynomial(): p1 = Polynomial([1, 2, 1]) p2 = Polynomial([2, 2, 3]) p3 = p1 - p2 assert p3.coeff == [-1, 0, -2] p1 = Polynomial([1, 2, 1]) p2 = Polynomial([2, 2]) p3 = p1 - p2 assert p3.coeff == [-1, 0, 1] p1 = Polynomial([1, 2]) p2 = Polynomial([2, 2, 3]) p3 = p1 - p2 assert p3.coeff == [-1, 0, -3] test_Polynomial() print("Everything is good")