1
1
forked from kalmar/DALGLI0

Zaktualizuj 'wielomiany.py'

This commit is contained in:
Szymon Wojciechowski 2018-06-07 08:06:57 +00:00
parent 448979c654
commit 78f79da83c

View File

@ -1,10 +1,13 @@
import sys
import ast
class Polynomial:
n = 0
def __init__(self, *args):
self.degree = len(args) - 1
self.coefficients = list(args)
def __init__(self, coef_list):
self.degree = len(coef_list) - 1
self.coefficients = coef_list
@staticmethod
def multiply(p1, p2):
@ -15,7 +18,7 @@ class Polynomial:
for j in range(0, len(g)):
result[i+j] += f[i] * g[j]
result = [x % int(n) for x in result]
return Polynomial(*result)
return Polynomial(result)
@staticmethod
def divide(p1, p2):
@ -41,13 +44,13 @@ class Polynomial:
for i in range(tmp_exp):
tmp.append(0)
tmp.append(tmp_coef)
tmp_poly = Polynomial(*tmp)
tmp_poly = Polynomial(tmp)
sub = Polynomial.multiply(p2, tmp_poly)
f = [x - y for x, y in zip(f, sub.coefficients)]
f = [x % int(n) for x in f]
while f and f[-1] == 0:
f.pop()
return Polynomial(*f)
return Polynomial(f)
@staticmethod
def gcd(p1, p2):
@ -56,32 +59,32 @@ class Polynomial:
return Polynomial.gcd(p2, Polynomial.divide(p1, p2))
n = input("Enter n: ")
Polynomial.n = int(n)
def main():
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()]
c1 = [x % Polynomial.n for x in c1]
c2 = [int(x) for x in input("Enter 2nd polynomial: ").split()]
c2 = [x % Polynomial.n for x in c2]
f = Polynomial(c1)
g = Polynomial(c2)
f = Polynomial(*c1)
g = Polynomial(*c2)
ans = []
ans = []
mul = Polynomial.multiply(f, g)
ans.append(mul.coefficients)
mul = Polynomial.multiply(f, g)
ans.append(mul.coefficients)
try:
div = Polynomial.divide(f, g)
ans.append(div.coefficients)
except ZeroDivisionError as e:
ans.append(e)
try:
div = Polynomial.divide(f, g)
ans.append(div.coefficients)
except ZeroDivisionError as e:
ans.append(e)
try:
gcd = Polynomial.gcd(f, g)
ans.append(gcd.coefficients)
except ZeroDivisionError as e:
ans.append(e)
try:
gcd = Polynomial.gcd(f, g)
ans.append(gcd.coefficients)
except ZeroDivisionError as e:
ans.append(e)
print(ans)
print(ans)
if __name__ == "__main__":
n = int(sys.argv[1])
main()