From 24087c2bb4a7023d4bbf3770c638faab82b6e287 Mon Sep 17 00:00:00 2001 From: Szymon Wojciechowski Date: Wed, 30 May 2018 18:35:47 +0000 Subject: [PATCH 01/17] =?UTF-8?q?Prze=C5=9Blij=20pliki=20do=20''?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Ring.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Ring.py diff --git a/Ring.py b/Ring.py new file mode 100644 index 0000000..88345d4 --- /dev/null +++ b/Ring.py @@ -0,0 +1,48 @@ +from fractions import gcd + +class Modulo: + + def __init__(self, n): + self.n = int(n) + self.modulo_set = list(range(self.n)) + self.answer = [] + + def get_inverse_elements(self): + self.inverse_elements = [] + for i in self.modulo_set: + if gcd(i, self.n) == 1: + self.inverse_elements.append(i) + self.answer.append(self.inverse_elements) + + def get_zero_divisors(self): + self.zero_divisors = [] + for i in self.modulo_set: + for j in self.modulo_set: + if (i * j) % self.n == 0 and i != 0 and j != 0: + self.zero_divisors.append(i) + self.answer.append(list(set(self.zero_divisors))) + + def get_nilpotent_elements(self): + self.nilpotent_elements = [] + for i in self.modulo_set: + for j in range(1, len(self.inverse_elements) + 1): + if (i**j) % self.n == 0 and i != 0: + self.nilpotent_elements.append(i) + self.answer.append(list(set(self.nilpotent_elements))) + + def get_idempotent_elements(self): + self.idempotent_elements = [] + for i in self.modulo_set: + if (i*i) % self.n == i: + self.idempotent_elements.append(i) + self.answer.append(self.idempotent_elements) + + def get_all(self): + self.get_inverse_elements() + self.get_zero_divisors() + self.get_nilpotent_elements() + self.get_idempotent_elements() + return self.answer + +s = Modulo(input("Enter n: ")) +print(s.get_all()) -- 2.20.1 From 448979c654b0966b86ebaf8a8d4d94c1013aa6e5 Mon Sep 17 00:00:00 2001 From: Szymon Wojciechowski Date: Wed, 6 Jun 2018 23:25:32 +0000 Subject: [PATCH 02/17] Dodaj 'wielomiany.py' --- wielomiany.py | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 wielomiany.py diff --git a/wielomiany.py b/wielomiany.py new file mode 100644 index 0000000..c00ab5f --- /dev/null +++ b/wielomiany.py @@ -0,0 +1,87 @@ +class Polynomial: + + n = 0 + + def __init__(self, *args): + self.degree = len(args) - 1 + self.coefficients = list(args) + + @staticmethod + def multiply(p1, p2): + result = [0] * (p1.degree + p2.degree + 1) + f = p1.coefficients + g = p2.coefficients + for i in range(0, len(f)): + 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) + + @staticmethod + def divide(p1, p2): + def inverse(x): + for i in range(1, int(n)): + r = (i * x) % int(n) + if r == 1: + break + else: + raise ZeroDivisionError + return i + if p1.degree < p2.degree: + return p1 + f = p1.coefficients + g = p2.coefficients + g_lead_coef = g[-1] + g_deg = p2.degree + while len(f) >= len(g): + f_lead_coef = f[-1] + tmp_coef = f_lead_coef * inverse(g_lead_coef) + tmp_exp = len(f) - 1 - g_deg + tmp = [] + for i in range(tmp_exp): + tmp.append(0) + tmp.append(tmp_coef) + 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) + + @staticmethod + def gcd(p1, p2): + if len(p2.coefficients) == 0: + return p1 + return Polynomial.gcd(p2, Polynomial.divide(p1, p2)) + + +n = input("Enter n: ") +Polynomial.n = int(n) + +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) + +ans = [] + +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: + gcd = Polynomial.gcd(f, g) + ans.append(gcd.coefficients) +except ZeroDivisionError as e: + ans.append(e) + +print(ans) \ No newline at end of file -- 2.20.1 From 78f79da83cc4fc43b6840de833447a1b0c5dc974 Mon Sep 17 00:00:00 2001 From: Szymon Wojciechowski Date: Thu, 7 Jun 2018 08:06:57 +0000 Subject: [PATCH 03/17] Zaktualizuj 'wielomiany.py' --- wielomiany.py | 59 +++++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/wielomiany.py b/wielomiany.py index c00ab5f..833e9d6 100644 --- a/wielomiany.py +++ b/wielomiany.py @@ -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) \ No newline at end of file +if __name__ == "__main__": + n = int(sys.argv[1]) + main() \ No newline at end of file -- 2.20.1 From 0cee287283b3e867d6db404035ca1a0c055ecf5f Mon Sep 17 00:00:00 2001 From: Szymon Wojciechowski Date: Wed, 20 Jun 2018 15:28:24 +0000 Subject: [PATCH 04/17] Dodaj 'crc16.py' --- crc16.py | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 crc16.py diff --git a/crc16.py b/crc16.py new file mode 100644 index 0000000..bd75c00 --- /dev/null +++ b/crc16.py @@ -0,0 +1,139 @@ +import sys +import ast +import time +from itertools import zip_longest + +class Polynomial: + + def __init__(self, coef_list): + self.degree = len(coef_list) - 1 + self.coefficients = coef_list + + @staticmethod + def add(p1, p2): + result = [] + f = p1.coefficients + g = p2.coefficients + if len(f) >= len(g): + result = f + for i in range(0, len(g)): + result[i] = f[i] + g[i] + else: + result = g + for i in range(0, len(f)): + result[i] = f[i] + g[i] + result = [x % int(n) for x in result] + return Polynomial(result) + + @staticmethod + def multiply(p1, p2): + result = [0] * (p1.degree + p2.degree + 1) + f = p1.coefficients + g = p2.coefficients + for i in range(0, len(f)): + 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) + + @staticmethod + def divide(p1, p2): + def inverse(x): + for i in range(1, int(n)): + r = (i * x) % int(n) + if r == 1: + break + else: + raise ZeroDivisionError + return i + if p1.degree < p2.degree: + return p1 + f = p1.coefficients + g = p2.coefficients + g_lead_coef = g[-1] + g_deg = p2.degree + while len(f) >= len(g): + f_lead_coef = f[-1] + tmp_coef = f_lead_coef * inverse(g_lead_coef) + tmp_exp = len(f) - 1 - g_deg + tmp = [] + for _ in range(tmp_exp): + tmp.append(0) + tmp.append(tmp_coef) + 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) + + +n = 2 + + +def normalize_byte(data): + while len(data) != 8: + if len(data) < 8: + data = str(0) + data + else: + data.replace(data[0], '') + return data + + +def encode(in_data): + data = in_data + data = list(data) + data_binary = [bin(ord(char)).replace('b', '') for char in data] + data_binary = [normalize_byte(byte) for byte in data_binary] + data_binary = [int(bit) for bit in list(''.join(data_binary))] + data_binary.reverse() + + M = [0] * 16 + data_binary + L = [0] * (len(data_binary)) + [1] * 16 + G = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1] + FCS_poly = Polynomial.divide(Polynomial.add(Polynomial(M), Polynomial(L)), Polynomial(G)) + FCS = FCS_poly.coefficients + [0] * (16 - len(FCS_poly.coefficients)) + + FCS.reverse() + data_binary.reverse() + data_binary = data_binary + FCS + data_binary = [data_binary[i:i+8] for i in range(0, len(data_binary), 8)] + data = [chr(int(''.join(map(str, byte)), 2)) for byte in data_binary] + + return ''.join(data) + + +def check_fcs(in_data): + data = in_data + data = list(data) + data_binary = [bin(ord(char)).replace('b', '') for char in data] + data_binary = [normalize_byte(byte) for byte in data_binary] + data_binary = [int(bit) for bit in list(''.join(data_binary))] + data_binary.reverse() + + C = [0] * 16 + data_binary + L = [0] * len(data_binary) + [1] * 16 + G = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1] + S = Polynomial.divide(Polynomial.add(Polynomial(C), Polynomial(L)), Polynomial(G)) + + if S.coefficients == []: + return True + + return False + + +def main(): + try: + if sys.argv[1] == '-e' or sys.argv[1] == '-encode': + print(encode(sys.argv[2])) + elif sys.argv[1] == '-c' or sys.argv[1] == '-check': + print(check_fcs(sys.argv[2])) + else: + raise IndexError + except IndexError: + print("To encode: python3 CRC16.py -e [argument]\nTo check FCS: python3 CRC16.py -c [argument]") + + +if __name__ == "__main__": + main() -- 2.20.1 From 772736120b54377dc291118984ca775689d6ac10 Mon Sep 17 00:00:00 2001 From: Szymon Wojciechowski Date: Mon, 25 Jun 2018 15:21:17 +0000 Subject: [PATCH 05/17] bugfix --- crc16.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crc16.py b/crc16.py index bd75c00..f3d1777 100644 --- a/crc16.py +++ b/crc16.py @@ -1,7 +1,5 @@ import sys import ast -import time -from itertools import zip_longest class Polynomial: @@ -77,7 +75,7 @@ def normalize_byte(data): if len(data) < 8: data = str(0) + data else: - data.replace(data[0], '') + data = data[1:] return data @@ -126,7 +124,9 @@ def check_fcs(in_data): def main(): try: if sys.argv[1] == '-e' or sys.argv[1] == '-encode': - print(encode(sys.argv[2])) + arg = encode(sys.argv[2]) + print(arg) + print(check_fcs(arg)) # powinno zawsze zwracać true elif sys.argv[1] == '-c' or sys.argv[1] == '-check': print(check_fcs(sys.argv[2])) else: @@ -136,4 +136,4 @@ def main(): if __name__ == "__main__": - main() + main() \ No newline at end of file -- 2.20.1 From 09dcfac9ab436cf8ae72d5fac712bd8710f21553 Mon Sep 17 00:00:00 2001 From: Szymon Wojciechowski Date: Mon, 25 Jun 2018 17:54:51 +0000 Subject: [PATCH 06/17] changed input --- crc16.py | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/crc16.py b/crc16.py index f3d1777..ade6f26 100644 --- a/crc16.py +++ b/crc16.py @@ -90,24 +90,34 @@ def encode(in_data): M = [0] * 16 + data_binary L = [0] * (len(data_binary)) + [1] * 16 G = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1] - FCS_poly = Polynomial.divide(Polynomial.add(Polynomial(M), Polynomial(L)), Polynomial(G)) - FCS = FCS_poly.coefficients + [0] * (16 - len(FCS_poly.coefficients)) + result_poly = Polynomial.divide(Polynomial.add(Polynomial(M), Polynomial(L)), Polynomial(G)) + result = result_poly.coefficients + [0] * (16 - len(result_poly.coefficients)) - FCS.reverse() + result.reverse() data_binary.reverse() - data_binary = data_binary + FCS + data_binary = data_binary + result data_binary = [data_binary[i:i+8] for i in range(0, len(data_binary), 8)] - data = [chr(int(''.join(map(str, byte)), 2)) for byte in data_binary] + data_binary = [int(''.join(map(str, byte)), 2) for byte in data_binary] - return ''.join(data) + fcs = [hex(byte) for byte in data_binary] + fcs = fcs[-2:] + + data = [chr(byte) for byte in data_binary] + data = ''.join(data[0:-2]) + + return data, fcs -def check_fcs(in_data): +def check_fcs(in_data, fcs): data = in_data data = list(data) data_binary = [bin(ord(char)).replace('b', '') for char in data] data_binary = [normalize_byte(byte) for byte in data_binary] data_binary = [int(bit) for bit in list(''.join(data_binary))] + fcs_binary = [bin(int(in_hex, 16)).replace('b', '') for in_hex in fcs] + fcs_binary = [normalize_byte(byte) for byte in fcs_binary] + fcs_binary = [int(bit) for bit in list(''.join(fcs_binary))] + data_binary += fcs_binary data_binary.reverse() C = [0] * 16 + data_binary @@ -124,15 +134,17 @@ def check_fcs(in_data): def main(): try: if sys.argv[1] == '-e' or sys.argv[1] == '-encode': - arg = encode(sys.argv[2]) - print(arg) - print(check_fcs(arg)) # powinno zawsze zwracać true + arg, fcs = encode(sys.argv[2]) + print(arg, fcs) + print(check_fcs(arg, fcs)) # powinno zawsze zwracać true elif sys.argv[1] == '-c' or sys.argv[1] == '-check': - print(check_fcs(sys.argv[2])) + arg = sys.argv[2] + fcs = ast.literal_eval(sys.argv[3]) + print(check_fcs(arg, fcs)) else: raise IndexError except IndexError: - print("To encode: python3 CRC16.py -e [argument]\nTo check FCS: python3 CRC16.py -c [argument]") + print("To encode: python3 CRC16.py -e [argument]\nTo check result: python3 CRC16.py -c [argument] [fcs]") if __name__ == "__main__": -- 2.20.1 From bc3dcdaabe2971fc0328316e7a898e8a7bf64c55 Mon Sep 17 00:00:00 2001 From: Szymon Wojciechowski Date: Wed, 27 Jun 2018 17:29:48 +0000 Subject: [PATCH 07/17] added polynomial addition --- wielomiany.py => poly.py | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) rename wielomiany.py => poly.py (73%) diff --git a/wielomiany.py b/poly.py similarity index 73% rename from wielomiany.py rename to poly.py index 833e9d6..2f335ff 100644 --- a/wielomiany.py +++ b/poly.py @@ -9,6 +9,22 @@ class Polynomial: self.degree = len(coef_list) - 1 self.coefficients = coef_list + @staticmethod + def add(p1, p2): + result = [] + f = p1.coefficients + g = p2.coefficients + if len(f) >= len(g): + result = f + for i in range(0, len(g)): + result[i] = f[i] + g[i] + else: + result = g + for i in range(0, len(f)): + result[i] = f[i] + g[i] + result = [x % int(Polynomial.n) for x in result] + return Polynomial(result) + @staticmethod def multiply(p1, p2): result = [0] * (p1.degree + p2.degree + 1) @@ -17,14 +33,14 @@ class Polynomial: for i in range(0, len(f)): for j in range(0, len(g)): result[i+j] += f[i] * g[j] - result = [x % int(n) for x in result] + result = [x % int(Polynomial.n) for x in result] return Polynomial(result) @staticmethod def divide(p1, p2): def inverse(x): - for i in range(1, int(n)): - r = (i * x) % int(n) + for i in range(1, int(Polynomial.n)): + r = (i * x) % int(Polynomial.n) if r == 1: break else: @@ -41,13 +57,13 @@ class Polynomial: tmp_coef = f_lead_coef * inverse(g_lead_coef) tmp_exp = len(f) - 1 - g_deg tmp = [] - for i in range(tmp_exp): + for _ in range(tmp_exp): tmp.append(0) tmp.append(tmp_coef) 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] + f = [x % int(Polynomial.n) for x in f] while f and f[-1] == 0: f.pop() return Polynomial(f) @@ -60,6 +76,7 @@ class Polynomial: def main(): + Polynomial.n = int(sys.argv[1]) c1 = ast.literal_eval(sys.argv[2]) c2 = ast.literal_eval(sys.argv[3]) @@ -86,5 +103,4 @@ def main(): print(ans) if __name__ == "__main__": - n = int(sys.argv[1]) - main() \ No newline at end of file + main() -- 2.20.1 From f76cc9e06d4f0a1b509b8f9d565849cefcb81865 Mon Sep 17 00:00:00 2001 From: Szymon Wojciechowski Date: Wed, 27 Jun 2018 17:38:32 +0000 Subject: [PATCH 08/17] import poly --- crc16.py | 70 ++------------------------------------------------------ 1 file changed, 2 insertions(+), 68 deletions(-) diff --git a/crc16.py b/crc16.py index ade6f26..0a44f74 100644 --- a/crc16.py +++ b/crc16.py @@ -1,74 +1,8 @@ +from poly import Polynomial import sys import ast -class Polynomial: - - def __init__(self, coef_list): - self.degree = len(coef_list) - 1 - self.coefficients = coef_list - - @staticmethod - def add(p1, p2): - result = [] - f = p1.coefficients - g = p2.coefficients - if len(f) >= len(g): - result = f - for i in range(0, len(g)): - result[i] = f[i] + g[i] - else: - result = g - for i in range(0, len(f)): - result[i] = f[i] + g[i] - result = [x % int(n) for x in result] - return Polynomial(result) - - @staticmethod - def multiply(p1, p2): - result = [0] * (p1.degree + p2.degree + 1) - f = p1.coefficients - g = p2.coefficients - for i in range(0, len(f)): - 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) - - @staticmethod - def divide(p1, p2): - def inverse(x): - for i in range(1, int(n)): - r = (i * x) % int(n) - if r == 1: - break - else: - raise ZeroDivisionError - return i - if p1.degree < p2.degree: - return p1 - f = p1.coefficients - g = p2.coefficients - g_lead_coef = g[-1] - g_deg = p2.degree - while len(f) >= len(g): - f_lead_coef = f[-1] - tmp_coef = f_lead_coef * inverse(g_lead_coef) - tmp_exp = len(f) - 1 - g_deg - tmp = [] - for _ in range(tmp_exp): - tmp.append(0) - tmp.append(tmp_coef) - 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) - - -n = 2 - +Polynomial.n = 2 def normalize_byte(data): while len(data) != 8: -- 2.20.1 From 965b5a039490ff49cb6257956f8eae147a7b1e5a Mon Sep 17 00:00:00 2001 From: Szymon Wojciechowski Date: Wed, 27 Jun 2018 17:43:04 +0000 Subject: [PATCH 09/17] Update 'poly.py' --- poly.py | 33 +-------------------------------- 1 file changed, 1 insertion(+), 32 deletions(-) diff --git a/poly.py b/poly.py index 2f335ff..9ace2ef 100644 --- a/poly.py +++ b/poly.py @@ -72,35 +72,4 @@ class Polynomial: def gcd(p1, p2): if len(p2.coefficients) == 0: return p1 - return Polynomial.gcd(p2, Polynomial.divide(p1, p2)) - - -def main(): - Polynomial.n = int(sys.argv[1]) - c1 = ast.literal_eval(sys.argv[2]) - c2 = ast.literal_eval(sys.argv[3]) - - f = Polynomial(c1) - g = Polynomial(c2) - - ans = [] - - 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: - gcd = Polynomial.gcd(f, g) - ans.append(gcd.coefficients) - except ZeroDivisionError as e: - ans.append(e) - - print(ans) - -if __name__ == "__main__": - main() + return Polynomial.gcd(p2, Polynomial.divide(p1, p2)) \ No newline at end of file -- 2.20.1 From 074d255dabf29fb5320ca087fefa2d3082e5daf8 Mon Sep 17 00:00:00 2001 From: Szymon Wojciechowski Date: Wed, 27 Jun 2018 19:59:00 +0000 Subject: [PATCH 10/17] Update 'poly.py' --- poly.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/poly.py b/poly.py index 9ace2ef..d9ddd32 100644 --- a/poly.py +++ b/poly.py @@ -7,7 +7,7 @@ class Polynomial: def __init__(self, coef_list): self.degree = len(coef_list) - 1 - self.coefficients = coef_list + self.coefficients = [x % Polynomial.n for x in coef_list] @staticmethod def add(p1, p2): @@ -72,4 +72,4 @@ class Polynomial: def gcd(p1, p2): if len(p2.coefficients) == 0: return p1 - return Polynomial.gcd(p2, Polynomial.divide(p1, p2)) \ No newline at end of file + return Polynomial.gcd(p2, Polynomial.divide(p1, p2)) -- 2.20.1 From 39addf92dafb4ed1f22f340e7b90cdbd5c421fa3 Mon Sep 17 00:00:00 2001 From: Szymon Wojciechowski Date: Thu, 28 Jun 2018 08:57:27 +0000 Subject: [PATCH 11/17] Upload files to 'Zadanie-4' --- Zadanie-4/poly.py | 83 +++++++++++++++++++++++++++++++++++ Zadanie-4/quotient_ring.py | 88 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 Zadanie-4/poly.py create mode 100644 Zadanie-4/quotient_ring.py diff --git a/Zadanie-4/poly.py b/Zadanie-4/poly.py new file mode 100644 index 0000000..ab3ac44 --- /dev/null +++ b/Zadanie-4/poly.py @@ -0,0 +1,83 @@ +import sys +import ast + +class Polynomial: + + n = 0 + + def __init__(self, coeff_list): + self.degree = len(coeff_list) - 1 + self.coefficients = [x % Polynomial.n for x in coeff_list] + + def __pow__(self, n): + result = self + for i in range(n): + result = Polynomial.multiply(result, result) + return result + + @staticmethod + def add(p1, p2): + result = [] + f = p1.coefficients + g = p2.coefficients + if len(f) >= len(g): + result = f + for i in range(len(g)): + result[i] = f[i] + g[i] + else: + result = g + for i in range(len(f)): + result[i] = f[i] + g[i] + result = [x % int(Polynomial.n) for x in result] + return Polynomial(result) + + @staticmethod + def multiply(p1, p2): + result = [0] * (p1.degree + p2.degree + 1) + f = p1.coefficients + g = p2.coefficients + for i in range(len(f)): + for j in range(len(g)): + result[i+j] += f[i] * g[j] + result = [x % int(Polynomial.n) for x in result] + return Polynomial(result) + + @staticmethod + def divide(p1, p2): + + def inverse(x): + for i in range(1, int(Polynomial.n)): + r = (i * x) % int(Polynomial.n) + if r == 1: + break + else: + raise ZeroDivisionError + return i + + if p1.degree < p2.degree: + return p1 + f = p1.coefficients + g = p2.coefficients + g_lead_coef = g[-1] + g_deg = p2.degree + while len(f) >= len(g): + f_lead_coef = f[-1] + tmp_coef = f_lead_coef * inverse(g_lead_coef) + tmp_exp = len(f) - 1 - g_deg + tmp = [] + for _ in range(tmp_exp): + tmp.append(0) + tmp.append(tmp_coef) + tmp_poly = Polynomial(tmp) + sub = Polynomial.multiply(p2, tmp_poly) + f = [x - y for x, y in zip(f, sub.coefficients)] + f = [x % int(Polynomial.n) for x in f] + while f and f[-1] == 0: + f.pop() + return Polynomial(f) + + @staticmethod + def gcd(p1, p2): + if len(p2.coefficients) == 0: + return p1 + return Polynomial.gcd(p2, Polynomial.divide(p1, p2)) diff --git a/Zadanie-4/quotient_ring.py b/Zadanie-4/quotient_ring.py new file mode 100644 index 0000000..f9e963b --- /dev/null +++ b/Zadanie-4/quotient_ring.py @@ -0,0 +1,88 @@ +from poly import Polynomial as P +import sys +import ast + +class QuotientRing: + + def __init__(self, coeffs): + self.fx = P(coeffs) + self.remainder_set = self.create_remainder_set() + self.invertible_elements = self.get_invertible_elements() + self.zero_divisors = self.get_zero_divisors() + self.nilpotent_elements = self.get_nilpotent_elements() + self.idempotent_elements = self.get_idempotent_elements() + + def create_remainder_set(self): + remainders = [] + rem = [0] + i = 0 + while len(rem) < len(self.fx.coefficients): + remainders.append(P(rem)) + i = (i + 1) % P.n + rem[0] = i + if i == 0: + if len(rem) == 1: + rem.append(1) + else: + rem[1] += 1 + for j in range(1, len(rem)): + if rem[j] == 0 or rem[j] % P.n != 0: + break + tmp = rem[j] % P.n + rem[j] = 0 + if tmp == 0: + if (j + 1) < len(rem): + rem[j+1] += 1 + else: + rem.append(1) + return remainders + + def get_invertible_elements(self): + invertible_elements = [] + for i in self.remainder_set: + if i.coefficients != [0] and len(P.gcd(self.fx, i).coefficients) == 1: + invertible_elements.append(i) + return invertible_elements + + def get_zero_divisors(self): + zero_diviors = [] + for i in self.remainder_set: + if i not in self.invertible_elements: + zero_diviors.append(i) + return zero_diviors + + def get_nilpotent_elements(self): + nilpotent_elements = [] + for i in self.zero_divisors: + for j in range(1, len(self.invertible_elements) + 1): + if i.coefficients == [0] or len(P.divide(i**j, self.fx).coefficients) == 0: + nilpotent_elements.append(i) + break + return nilpotent_elements + + def get_idempotent_elements(self): + idempotent_elements = [] + for i in self.remainder_set: + if P.divide(i**2, self.fx).coefficients == P.divide(i, self.fx).coefficients: + idempotent_elements.append(i) + return idempotent_elements + + +def main(): + P.n = int(sys.argv[1]) + coeffs = ast.literal_eval(sys.argv[2]) + Q = QuotientRing(coeffs) + + ans = [ + [x.coefficients for x in Q.invertible_elements], + [x.coefficients for x in Q.zero_divisors], + [x.coefficients for x in Q.nilpotent_elements], + [x.coefficients for x in Q.idempotent_elements] + ] + + for i in range(len(ans)): + print(ans[i]) + + +if __name__ == '__main__': + main() \ No newline at end of file -- 2.20.1 From e3e55f9e0d07840af027604109f81fe8916dee67 Mon Sep 17 00:00:00 2001 From: Szymon Wojciechowski Date: Thu, 28 Jun 2018 09:01:41 +0000 Subject: [PATCH 12/17] Delete '02-Wielomiany.md' --- 02-Wielomiany.md | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 02-Wielomiany.md diff --git a/02-Wielomiany.md b/02-Wielomiany.md deleted file mode 100644 index 4e9830d..0000000 --- a/02-Wielomiany.md +++ /dev/null @@ -1,19 +0,0 @@ -## Zadanie - -Napisać program, który dla danego pierścienia współczynników `R = ℤ/nℤ, n ∈ ℕ` oraz wielomianów `f,g ∈ R[x] ` zmiennej `x ` znajdzie: - -1. iloczyn `f⋅g ∈ R[x]` -2. klasę reszty `f ∈ R[x]/(g)` -3. największy wspólny dzielnik `nwd(f,g)` korzystając z algorytmu Euklidesa. - -**Uwaga**: wielomiany są podawane jako ciąg współczynników **od wyrazu wolnego, do współczynnika wiodącego**. - -Termin: 07.06 - -### Przykłady: - -> Input: `2, [1,1,1,0,1], [0,1,1]` (i.e. `f = 1 + x + x² + x⁴, g = x² + x`) -> Output: `[[0,1,0,0,1,1,1], [1,1], [1,1]]` - -> Input: `6, [2,1,0,2,1,3], [1,0,0,5]` -> Output: `[[3,1,0,5,0,1,4,5,5], [5,2,1], DivisionError]` -- 2.20.1 From 12abe0d15680a48b30342d1bb70ab215a9294acc Mon Sep 17 00:00:00 2001 From: Szymon Wojciechowski Date: Thu, 28 Jun 2018 09:01:54 +0000 Subject: [PATCH 13/17] =?UTF-8?q?Delete=20'01-Pier=C5=9Bcie=C5=84-Zn.md'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 01-Pierścień-Zn.md | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100644 01-Pierścień-Zn.md diff --git a/01-Pierścień-Zn.md b/01-Pierścień-Zn.md deleted file mode 100644 index 93c5417..0000000 --- a/01-Pierścień-Zn.md +++ /dev/null @@ -1,21 +0,0 @@ -## Zadanie - -Napisać algorytm, który dla danego `n ∈ ℕ` znajdzie wszystkie: - - 1. elementy odwracalne - 2. dzielniki zera - 3. elementy nilpotentne - 4. elementy idempotentne - -w pierścieniu `{ℤ/nℤ, +, ⋅}`. - -Termin: 31.05 - -### Przykłady: - -> Input: `4` -> Output: `[[1,3], [0,2], [0,2], [0,1]]` - -> Input: `6` -> Output: `[[1,5], [0,2,3,4], [0], [0,1,3,4]]` - -- 2.20.1 From db0b78d7715f350394c5427811d671bd5c25b334 Mon Sep 17 00:00:00 2001 From: Szymon Wojciechowski Date: Thu, 28 Jun 2018 09:02:32 +0000 Subject: [PATCH 14/17] Delete 'poly.py' --- poly.py | 75 --------------------------------------------------------- 1 file changed, 75 deletions(-) delete mode 100644 poly.py diff --git a/poly.py b/poly.py deleted file mode 100644 index d9ddd32..0000000 --- a/poly.py +++ /dev/null @@ -1,75 +0,0 @@ -import sys -import ast - -class Polynomial: - - n = 0 - - def __init__(self, coef_list): - self.degree = len(coef_list) - 1 - self.coefficients = [x % Polynomial.n for x in coef_list] - - @staticmethod - def add(p1, p2): - result = [] - f = p1.coefficients - g = p2.coefficients - if len(f) >= len(g): - result = f - for i in range(0, len(g)): - result[i] = f[i] + g[i] - else: - result = g - for i in range(0, len(f)): - result[i] = f[i] + g[i] - result = [x % int(Polynomial.n) for x in result] - return Polynomial(result) - - @staticmethod - def multiply(p1, p2): - result = [0] * (p1.degree + p2.degree + 1) - f = p1.coefficients - g = p2.coefficients - for i in range(0, len(f)): - for j in range(0, len(g)): - result[i+j] += f[i] * g[j] - result = [x % int(Polynomial.n) for x in result] - return Polynomial(result) - - @staticmethod - def divide(p1, p2): - def inverse(x): - for i in range(1, int(Polynomial.n)): - r = (i * x) % int(Polynomial.n) - if r == 1: - break - else: - raise ZeroDivisionError - return i - if p1.degree < p2.degree: - return p1 - f = p1.coefficients - g = p2.coefficients - g_lead_coef = g[-1] - g_deg = p2.degree - while len(f) >= len(g): - f_lead_coef = f[-1] - tmp_coef = f_lead_coef * inverse(g_lead_coef) - tmp_exp = len(f) - 1 - g_deg - tmp = [] - for _ in range(tmp_exp): - tmp.append(0) - tmp.append(tmp_coef) - tmp_poly = Polynomial(tmp) - sub = Polynomial.multiply(p2, tmp_poly) - f = [x - y for x, y in zip(f, sub.coefficients)] - f = [x % int(Polynomial.n) for x in f] - while f and f[-1] == 0: - f.pop() - return Polynomial(f) - - @staticmethod - def gcd(p1, p2): - if len(p2.coefficients) == 0: - return p1 - return Polynomial.gcd(p2, Polynomial.divide(p1, p2)) -- 2.20.1 From 79d5960ebdafaa9bab50d007d9cfcc8329e793d3 Mon Sep 17 00:00:00 2001 From: Szymon Wojciechowski Date: Thu, 28 Jun 2018 09:02:37 +0000 Subject: [PATCH 15/17] Delete 'crc16.py' --- crc16.py | 85 -------------------------------------------------------- 1 file changed, 85 deletions(-) delete mode 100644 crc16.py diff --git a/crc16.py b/crc16.py deleted file mode 100644 index 0a44f74..0000000 --- a/crc16.py +++ /dev/null @@ -1,85 +0,0 @@ -from poly import Polynomial -import sys -import ast - -Polynomial.n = 2 - -def normalize_byte(data): - while len(data) != 8: - if len(data) < 8: - data = str(0) + data - else: - data = data[1:] - return data - - -def encode(in_data): - data = in_data - data = list(data) - data_binary = [bin(ord(char)).replace('b', '') for char in data] - data_binary = [normalize_byte(byte) for byte in data_binary] - data_binary = [int(bit) for bit in list(''.join(data_binary))] - data_binary.reverse() - - M = [0] * 16 + data_binary - L = [0] * (len(data_binary)) + [1] * 16 - G = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1] - result_poly = Polynomial.divide(Polynomial.add(Polynomial(M), Polynomial(L)), Polynomial(G)) - result = result_poly.coefficients + [0] * (16 - len(result_poly.coefficients)) - - result.reverse() - data_binary.reverse() - data_binary = data_binary + result - data_binary = [data_binary[i:i+8] for i in range(0, len(data_binary), 8)] - data_binary = [int(''.join(map(str, byte)), 2) for byte in data_binary] - - fcs = [hex(byte) for byte in data_binary] - fcs = fcs[-2:] - - data = [chr(byte) for byte in data_binary] - data = ''.join(data[0:-2]) - - return data, fcs - - -def check_fcs(in_data, fcs): - data = in_data - data = list(data) - data_binary = [bin(ord(char)).replace('b', '') for char in data] - data_binary = [normalize_byte(byte) for byte in data_binary] - data_binary = [int(bit) for bit in list(''.join(data_binary))] - fcs_binary = [bin(int(in_hex, 16)).replace('b', '') for in_hex in fcs] - fcs_binary = [normalize_byte(byte) for byte in fcs_binary] - fcs_binary = [int(bit) for bit in list(''.join(fcs_binary))] - data_binary += fcs_binary - data_binary.reverse() - - C = [0] * 16 + data_binary - L = [0] * len(data_binary) + [1] * 16 - G = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1] - S = Polynomial.divide(Polynomial.add(Polynomial(C), Polynomial(L)), Polynomial(G)) - - if S.coefficients == []: - return True - - return False - - -def main(): - try: - if sys.argv[1] == '-e' or sys.argv[1] == '-encode': - arg, fcs = encode(sys.argv[2]) - print(arg, fcs) - print(check_fcs(arg, fcs)) # powinno zawsze zwracać true - elif sys.argv[1] == '-c' or sys.argv[1] == '-check': - arg = sys.argv[2] - fcs = ast.literal_eval(sys.argv[3]) - print(check_fcs(arg, fcs)) - else: - raise IndexError - except IndexError: - print("To encode: python3 CRC16.py -e [argument]\nTo check result: python3 CRC16.py -c [argument] [fcs]") - - -if __name__ == "__main__": - main() \ No newline at end of file -- 2.20.1 From 0947fadbdfb96f36862b7c123e27c402beb4cd4c Mon Sep 17 00:00:00 2001 From: Szymon Wojciechowski Date: Thu, 28 Jun 2018 09:02:51 +0000 Subject: [PATCH 16/17] Delete 'Ring.py' --- Ring.py | 48 ------------------------------------------------ 1 file changed, 48 deletions(-) delete mode 100644 Ring.py diff --git a/Ring.py b/Ring.py deleted file mode 100644 index 88345d4..0000000 --- a/Ring.py +++ /dev/null @@ -1,48 +0,0 @@ -from fractions import gcd - -class Modulo: - - def __init__(self, n): - self.n = int(n) - self.modulo_set = list(range(self.n)) - self.answer = [] - - def get_inverse_elements(self): - self.inverse_elements = [] - for i in self.modulo_set: - if gcd(i, self.n) == 1: - self.inverse_elements.append(i) - self.answer.append(self.inverse_elements) - - def get_zero_divisors(self): - self.zero_divisors = [] - for i in self.modulo_set: - for j in self.modulo_set: - if (i * j) % self.n == 0 and i != 0 and j != 0: - self.zero_divisors.append(i) - self.answer.append(list(set(self.zero_divisors))) - - def get_nilpotent_elements(self): - self.nilpotent_elements = [] - for i in self.modulo_set: - for j in range(1, len(self.inverse_elements) + 1): - if (i**j) % self.n == 0 and i != 0: - self.nilpotent_elements.append(i) - self.answer.append(list(set(self.nilpotent_elements))) - - def get_idempotent_elements(self): - self.idempotent_elements = [] - for i in self.modulo_set: - if (i*i) % self.n == i: - self.idempotent_elements.append(i) - self.answer.append(self.idempotent_elements) - - def get_all(self): - self.get_inverse_elements() - self.get_zero_divisors() - self.get_nilpotent_elements() - self.get_idempotent_elements() - return self.answer - -s = Modulo(input("Enter n: ")) -print(s.get_all()) -- 2.20.1 From fc718efb6ca62a5d895a46533dd416617037039b Mon Sep 17 00:00:00 2001 From: Szymon Wojciechowski Date: Thu, 28 Jun 2018 12:40:44 +0000 Subject: [PATCH 17/17] Update 'Zadanie-4/poly.py' --- Zadanie-4/poly.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Zadanie-4/poly.py b/Zadanie-4/poly.py index ab3ac44..8900371 100644 --- a/Zadanie-4/poly.py +++ b/Zadanie-4/poly.py @@ -11,7 +11,7 @@ class Polynomial: def __pow__(self, n): result = self - for i in range(n): + for _ in range(n): result = Polynomial.multiply(result, result) return result -- 2.20.1