forked from kalmar/DALGLI0
input z lini poleceń
This commit is contained in:
parent
1d1b6f026a
commit
3feef9f4af
139
zad2.py
139
zad2.py
@ -1,68 +1,71 @@
|
||||
def modulo(x,n):
|
||||
for i in range(len(x)):
|
||||
x[i] = x[i] % n
|
||||
return x
|
||||
|
||||
def iloczyn(f,g,n):
|
||||
wynik = [0]*(len(f)+len(g)-1)
|
||||
for i in range(len(f)):
|
||||
for j in range(len(g)):
|
||||
wynik[i+j] += f[i]*g[j]
|
||||
return modulo(wynik,n)
|
||||
|
||||
def liczmod(a,x,n):
|
||||
for i in range(n):
|
||||
if( (a*i) % n == x):
|
||||
return(i)
|
||||
return(-1)
|
||||
|
||||
def sumujmod(a,b,n):
|
||||
if(len(a) < len(b)):
|
||||
wynik = b
|
||||
for i in range(len(a)):
|
||||
wynik[i] += a[i]
|
||||
else:
|
||||
wynik = a
|
||||
for i in range(len(b)):
|
||||
wynik[i] += b[i]
|
||||
return modulo(wynik,n)
|
||||
|
||||
def redukcjazer(x):
|
||||
while x and x[-1] is 0:
|
||||
x.pop()
|
||||
return(x)
|
||||
|
||||
def klasaReszt(f,g,n):
|
||||
wynik = f
|
||||
mnoznik = [0] * (len(f)-len(g)+2)
|
||||
for i in range(len(f)):
|
||||
if(liczmod(g[len(g)-1],wynik[len(f)-1-i],n) == -1):
|
||||
return("DivisionError")
|
||||
if(i > len(f)-len(g)):
|
||||
return redukcjazer(modulo(wynik,n))
|
||||
else:
|
||||
mnoznik.pop()
|
||||
mnoznik.pop()
|
||||
mnoznik.append(-1*liczmod(g[len(g)-1],wynik[len(f)-1-i],n))
|
||||
wynik = sumujmod(wynik, iloczyn(g, mnoznik ,n) ,n)
|
||||
|
||||
def nwd(a,b,n):
|
||||
while(b != []):
|
||||
if(klasaReszt(a,b,n)):
|
||||
return "DivisionError"
|
||||
c = klasaReszt(a,b,n)
|
||||
a = b
|
||||
b = c
|
||||
return modulo(a,n)
|
||||
|
||||
def main():
|
||||
n = int(input("Podaj n: "))
|
||||
print("R[x] dla R = Z/"+str(n))
|
||||
f = eval(input("Podaj f w formacie listy np: [1,1, 0,1]: "))
|
||||
g = eval(input("Podaj f w formacie listy np: [1, 1,0,1]: "))
|
||||
print(iloczyn(f,g,n))
|
||||
print(klasaReszt(f,g,n))
|
||||
print(nwd(f,g,n))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
import sys
|
||||
import ast
|
||||
|
||||
def modulo(x,n):
|
||||
for i in range(len(x)):
|
||||
x[i] = x[i] % n
|
||||
return x
|
||||
|
||||
def iloczyn(f,g,n):
|
||||
wynik = [0]*(len(f)+len(g)-1)
|
||||
for i in range(len(f)):
|
||||
for j in range(len(g)):
|
||||
wynik[i+j] += f[i]*g[j]
|
||||
return modulo(wynik,n)
|
||||
|
||||
def liczmod(a,x,n):
|
||||
for i in range(n):
|
||||
if( (a*i) % n == x):
|
||||
return(i)
|
||||
return(-1)
|
||||
|
||||
def sumujmod(a,b,n):
|
||||
if(len(a) < len(b)):
|
||||
wynik = b
|
||||
for i in range(len(a)):
|
||||
wynik[i] += a[i]
|
||||
else:
|
||||
wynik = a
|
||||
for i in range(len(b)):
|
||||
wynik[i] += b[i]
|
||||
return modulo(wynik,n)
|
||||
|
||||
def redukcjazer(x):
|
||||
while x and x[-1] is 0:
|
||||
x.pop()
|
||||
return(x)
|
||||
|
||||
def klasaReszt(f,g,n):
|
||||
wynik = f
|
||||
mnoznik = [0] * (len(f)-len(g)+2)
|
||||
for i in range(len(f)):
|
||||
if(liczmod(g[len(g)-1],wynik[len(f)-1-i],n) == -1):
|
||||
return("DivisionError")
|
||||
if(i > len(f)-len(g)):
|
||||
return redukcjazer(modulo(wynik,n))
|
||||
else:
|
||||
mnoznik.pop()
|
||||
mnoznik.pop()
|
||||
mnoznik.append(-1*liczmod(g[len(g)-1],wynik[len(f)-1-i],n))
|
||||
wynik = sumujmod(wynik, iloczyn(g, mnoznik ,n) ,n)
|
||||
|
||||
def nwd(a,b,n):
|
||||
while(b != []):
|
||||
if(klasaReszt(a,b,n)):
|
||||
return "DivisionError"
|
||||
c = klasaReszt(a,b,n)
|
||||
a = b
|
||||
b = c
|
||||
return modulo(a,n)
|
||||
|
||||
def main():
|
||||
n = int(sys.argv[1])
|
||||
f = ast.literal_eval(sys.argv[2])
|
||||
g = ast.literal_eval(sys.argv[3])
|
||||
|
||||
print(iloczyn(f,g,n))
|
||||
print(klasaReszt(f,g,n))
|
||||
print(nwd(f,g,n))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Reference in New Issue
Block a user