-
Notifications
You must be signed in to change notification settings - Fork 3
/
polynomial.py
64 lines (54 loc) · 2.25 KB
/
polynomial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class Polynomial:
def __init__(self, coefficients):
# Remove leading zeros
while coefficients and coefficients[-1] == 0:
coefficients.pop()
self.coefficients = coefficients
def __str__(self):
if not self.coefficients:
return "0"
terms = []
for i, coef in enumerate(self.coefficients):
if coef != 0:
if i == 0:
terms.append(str(coef))
else:
if coef == 1:
term = f"x^{i}"
elif coef == -1:
term = f"-x^{i}"
else:
term = f"{coef}x^{i}"
terms.append(term)
return " + ".join(reversed(terms))
def degree(self):
if not self.coefficients:
return 0
return len(self.coefficients) - 1
def evaluate(self, x):
result = 0
for i, coef in enumerate(self.coefficients):
result += coef * (x ** i)
return result
def __add__(self, other):
# Pad the shorter polynomial with zeros
if len(self.coefficients) < len(other.coefficients):
self.coefficients += [0] * (len(other.coefficients) - len(self.coefficients))
else:
other.coefficients += [0] * (len(self.coefficients) - len(other.coefficients))
result_coeffs = [a + b for a, b in zip(self.coefficients, other.coefficients)]
return Polynomial(result_coeffs)
def __sub__(self, other):
# Pad the shorter polynomial with zeros
if len(self.coefficients) < len(other.coefficients):
self.coefficients += [0] * (len(other.coefficients) - len(self.coefficients))
else:
other.coefficients += [0] * (len(self.coefficients) - len(other.coefficients))
result_coeffs = [a - b for a, b in zip(self.coefficients, other.coefficients)]
return Polynomial(result_coeffs)
def __mul__(self, other):
result_coeffs = [0] * (len(self.coefficients) + len(other.coefficients) - 1)
for i, a in enumerate(self.coefficients):
for j, b in enumerate(other.coefficients):
result_coeffs[i + j] += a * b
return Polynomial(result_coeffs)