forked from kalmar/DALGLI0
Zaktualizuj 'wielomiany.py'
This commit is contained in:
parent
448979c654
commit
78f79da83c
@ -1,10 +1,13 @@
|
|||||||
|
import sys
|
||||||
|
import ast
|
||||||
|
|
||||||
class Polynomial:
|
class Polynomial:
|
||||||
|
|
||||||
n = 0
|
n = 0
|
||||||
|
|
||||||
def __init__(self, *args):
|
def __init__(self, coef_list):
|
||||||
self.degree = len(args) - 1
|
self.degree = len(coef_list) - 1
|
||||||
self.coefficients = list(args)
|
self.coefficients = coef_list
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def multiply(p1, p2):
|
def multiply(p1, p2):
|
||||||
@ -15,7 +18,7 @@ class Polynomial:
|
|||||||
for j in range(0, len(g)):
|
for j in range(0, len(g)):
|
||||||
result[i+j] += f[i] * g[j]
|
result[i+j] += f[i] * g[j]
|
||||||
result = [x % int(n) for x in result]
|
result = [x % int(n) for x in result]
|
||||||
return Polynomial(*result)
|
return Polynomial(result)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def divide(p1, p2):
|
def divide(p1, p2):
|
||||||
@ -41,13 +44,13 @@ class Polynomial:
|
|||||||
for i in range(tmp_exp):
|
for i in range(tmp_exp):
|
||||||
tmp.append(0)
|
tmp.append(0)
|
||||||
tmp.append(tmp_coef)
|
tmp.append(tmp_coef)
|
||||||
tmp_poly = Polynomial(*tmp)
|
tmp_poly = Polynomial(tmp)
|
||||||
sub = Polynomial.multiply(p2, tmp_poly)
|
sub = Polynomial.multiply(p2, tmp_poly)
|
||||||
f = [x - y for x, y in zip(f, sub.coefficients)]
|
f = [x - y for x, y in zip(f, sub.coefficients)]
|
||||||
f = [x % int(n) for x in f]
|
f = [x % int(n) for x in f]
|
||||||
while f and f[-1] == 0:
|
while f and f[-1] == 0:
|
||||||
f.pop()
|
f.pop()
|
||||||
return Polynomial(*f)
|
return Polynomial(f)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def gcd(p1, p2):
|
def gcd(p1, p2):
|
||||||
@ -56,32 +59,32 @@ class Polynomial:
|
|||||||
return Polynomial.gcd(p2, Polynomial.divide(p1, p2))
|
return Polynomial.gcd(p2, Polynomial.divide(p1, p2))
|
||||||
|
|
||||||
|
|
||||||
n = input("Enter n: ")
|
def main():
|
||||||
Polynomial.n = int(n)
|
c1 = ast.literal_eval(sys.argv[2])
|
||||||
|
c2 = ast.literal_eval(sys.argv[3])
|
||||||
|
|
||||||
c1 = [int(x) for x in input("Enter 1st polynomial (coefficients divided by space): ").split()]
|
f = Polynomial(c1)
|
||||||
c1 = [x % Polynomial.n for x in c1]
|
g = Polynomial(c2)
|
||||||
c2 = [int(x) for x in input("Enter 2nd polynomial: ").split()]
|
|
||||||
c2 = [x % Polynomial.n for x in c2]
|
|
||||||
|
|
||||||
f = Polynomial(*c1)
|
ans = []
|
||||||
g = Polynomial(*c2)
|
|
||||||
|
|
||||||
ans = []
|
mul = Polynomial.multiply(f, g)
|
||||||
|
ans.append(mul.coefficients)
|
||||||
|
|
||||||
mul = Polynomial.multiply(f, g)
|
try:
|
||||||
ans.append(mul.coefficients)
|
|
||||||
|
|
||||||
try:
|
|
||||||
div = Polynomial.divide(f, g)
|
div = Polynomial.divide(f, g)
|
||||||
ans.append(div.coefficients)
|
ans.append(div.coefficients)
|
||||||
except ZeroDivisionError as e:
|
except ZeroDivisionError as e:
|
||||||
ans.append(e)
|
ans.append(e)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
gcd = Polynomial.gcd(f, g)
|
gcd = Polynomial.gcd(f, g)
|
||||||
ans.append(gcd.coefficients)
|
ans.append(gcd.coefficients)
|
||||||
except ZeroDivisionError as e:
|
except ZeroDivisionError as e:
|
||||||
ans.append(e)
|
ans.append(e)
|
||||||
|
|
||||||
print(ans)
|
print(ans)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
n = int(sys.argv[1])
|
||||||
|
main()
|
Loading…
Reference in New Issue
Block a user