• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Python zad 8

a guest Dec 3rd, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import itertools as it
2.
3. class MyPolynomial(object):
4.   def __init__(self, *coefficients):
5.     self.coefficients = list(coefficients)
6.     self._priv = None
7.
8.   def __eq__(self, y):
9.     return str(self) == str(y)
10.
11.   def __repr__(self):
12.     if len(self.coefficients) == 0:
13.       return '{}({})'.format(
14.         self.__class__.__name__, '0'
15.         )
16.     if sum(self.coefficients) == 0:
17.       return '{}({})'.format(
18.         self.__class__.__name__, '0'
19.         )
20.     return '{}({})'.format(
21.       self.__class__.__name__, ', '.join((str(x) for x in self.coefficients))
22.       )
23.
24.   def __add__(self, other):
25.     n = len(self.coefficients)
26.     p = len(other.coefficients)
27.     if (n >= p):
28.       new_coefficients = [self.coefficients[i] for i in range(n)]
29.       for i in range(p):
30.         new_coefficients[i] += other.coefficients[i]
31.     if (p >= n):
32.       new_coefficients = [other.coefficients[i] for i in range(p)]
33.       for i in range(n):
34.         new_coefficients[i] += self.coefficients[i]
35.     return MyPolynomial(*new_coefficients)
36.
37.   def __iadd__(self, other):
38.     for i, x in enumerate(self.coefficients):
39.       self.coefficients[i] += other.coefficients[i]
40.     return self
41.
42.   def __str__(self):
43.     out = ''
44.     size = len(self.coefficients)
45.     for i in range(size):
46.         if self.coefficients[i] != 0:
47.             out += ' + %gx^%d' % (self.coefficients[i],i)
48.
49.     out = out.replace('+ -', '- ')
50.     out = out.replace('x^0', '1')
51.     out = out.replace(' 11 ', ' 1 ')
52.     if out[0:3] == ' + ':
53.         out = out[3:]
54.     if out[0:3] == ' - ':
55.         out = '-' + out[3:]
56.     if len(out) == 0:
57.       return '0'
58.     return out
59.
60.   def from_iterable(iterables):
61.     return MyPolynomial(*iterables)
62.
63.   def degree(self):
64.     if len(self.coefficients) == 0:
65.       return 0
66.     if sum(self.coefficients) == 0:
67.       return 0
68.     i = 1
69.     iterator = len(self.coefficients)-1
70.     print("iter:"+str(iterator))
71.     for x in self.coefficients:
72.       if self.coefficients[iterator] == 0:
73.         i = i + 1
74.       else:
75.         break
76.       iterator = iterator - 1
77.     return len(self.coefficients) - i
78.
79.   def __mul__(self, other):
80.
81.     if isinstance(other, (float, int)): other = MyPolynomial(other)
82.
83.     out = []
84.     for i in range(len(self.coefficients) + len(other.coefficients) - 1):
85.       out.append(0)
86.
87.     for i in range(0, len(self.coefficients)):
88.       for j in range(0, len(other.coefficients)):
89.         out[i+j] = out[i+j] + self.coefficients[i] * other.coefficients[j]
90.
91.     return MyPolynomial.from_iterable(out)
92.
93.   def __rmul__(self, other):
94.     return self * other
95.
96.   def __imul__(self, other):
97.     if isinstance(other, (float, int)): other = MyPolynomial(other)
98.     temp = self.coefficients
99.
100.     for i in range(len(temp)):
101.       self.coefficients[i] = 0
102.
103.     for i in range(len(other.coefficients)):
104.       self.coefficients.append(0)
105.
106.     for i in range(0, len(temp)):
107.       for j in range(0, len(other.coefficients)):
108.         self.coefficients[i+j] += temp[i] * other.coefficients[j]
109.
110.     return self
111.
112. assert MyPolynomial(6, 14, 8) == MyPolynomial(2, 2) * MyPolynomial(3, 4)
113. assert MyPolynomial(6, 14, 8) == MyPolynomial(3, 7, 4) * 2
114. assert MyPolynomial(6, 14, 8) == 2 * MyPolynomial(3, 7, 4)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top