From 66a3bb0671a1cd09257e537312abc7becfd3f2b3 Mon Sep 17 00:00:00 2001 From: Nicolas <nicolas.albanesi@etu.hesge.ch> Date: Tue, 14 Dec 2021 18:25:26 +0100 Subject: [PATCH] Changed indentation to tabs, modified get_possibilities --- inverse_mult.py | 10 +++--- main.py | 10 +++--- polynome.py | 82 ++++++++++++++++++++++++------------------------- reed_solomon.py | 6 ++-- 4 files changed, 54 insertions(+), 54 deletions(-) diff --git a/inverse_mult.py b/inverse_mult.py index b418792..8f74c4f 100644 --- a/inverse_mult.py +++ b/inverse_mult.py @@ -7,10 +7,10 @@ from euclide import * # x = Multiplicative inverse of a mod p: # a * x mod p = 1 def inverse_mult(a, p) : - pgcd, x, y = pgcd_etendu(a, p) - if pgcd != 1: - return None - return x if a > p else y + pgcd, x, y = pgcd_etendu(a, p) + if pgcd != 1: + return None + return x if a > p else y if __name__ == "__main__": - print(inverse_mult(3, 11)) \ No newline at end of file + print(inverse_mult(3, 11)) \ No newline at end of file diff --git a/main.py b/main.py index 32da3ec..f0006fb 100644 --- a/main.py +++ b/main.py @@ -3,8 +3,8 @@ from polynome import * if __name__ == "__main__": - pol1 = polynome([1, 2, 3, 4, 5]) - pol2 = polynome([2, 3, 4, 4, 4]) - # pol3 = pol1.add(pol2) - pol1.show() - pol1.evaluate(2) \ No newline at end of file + pol1 = polynome([1, 2, 3, 4, 5]) + pol2 = polynome([2, 3, 4, 4, 4]) + # pol3 = pol1.add(pol2) + pol1.show() + pol1.evaluate(2) \ No newline at end of file diff --git a/polynome.py b/polynome.py index 2f8be27..af51ff9 100644 --- a/polynome.py +++ b/polynome.py @@ -4,46 +4,46 @@ import math class polynome(): - def __init__(self, coefs : list): - self.coefs = coefs - self.prime_mod = 229 + def __init__(self, coefs : list): + self.coefs = coefs + self.prime_mod = 229 - def show(self): - for i in range(len(self.coefs) - 1, -1, -1): - power = "" - if self.coefs[i] != 0: - coef = str(self.coefs[i]) - if i != 0: # its not 0's degree - power = f"x^{i} + " - else: - coef = "" - print(f"{coef}{power}", end="") - print() # new line - - def add(self, poly_2): - coeff_poly_1 = self.coefs - if len(coeff_poly_1) > len(poly_2.coefs): - while len(coeff_poly_1) > len(poly_2.coefs): - poly_2.coefs.append(0) - else: - while len(coeff_poly_1) < len(poly_2.coefs): - coeff_poly_1.append(0) - - for i in range(0, len(coeff_poly_1)): - coeff_poly_1[i] = (coeff_poly_1[i] + poly_2.coefs[i]) % self.prime_mod - - return polynome(coeff_poly_1) + def show(self): + for i in range(len(self.coefs) - 1, -1, -1): + power = "" + if self.coefs[i] != 0: + coef = str(self.coefs[i]) + if i != 0: # its not 0's degree + power = f"x^{i} + " + else: + coef = "" + print(f"{coef}{power}", end="") + print() # new line + + def add(self, poly_2): + coeff_poly_1 = self.coefs + if len(coeff_poly_1) > len(poly_2.coefs): + while len(coeff_poly_1) > len(poly_2.coefs): + poly_2.coefs.append(0) + else: + while len(coeff_poly_1) < len(poly_2.coefs): + coeff_poly_1.append(0) + + for i in range(0, len(coeff_poly_1)): + coeff_poly_1[i] = (coeff_poly_1[i] + poly_2.coefs[i]) % self.prime_mod + + return polynome(coeff_poly_1) - def mul(self, poly_2): - coeff_poly_res = [0] * (len(self.coefs) + len(poly_2.coefs) - 1) - for index_1, value_1 in enumerate(self.coefs): - for index_2, value_2 in enumerate(poly_2.coefs): - coeff_poly_res[index_1 + index_2] += (value_1 * value_2) % self.prime_mod - return polynome(coeff_poly_res) - - def evaluate(self, x): - # Using horner method - res = 0 - for i in range(len(self.coefs) - 1, -1, -1): - res = (res * x + self.coefs[i]) - return (res % self.prime_mod) + def mul(self, poly_2): + coeff_poly_res = [0] * (len(self.coefs) + len(poly_2.coefs) - 1) + for index_1, value_1 in enumerate(self.coefs): + for index_2, value_2 in enumerate(poly_2.coefs): + coeff_poly_res[index_1 + index_2] += (value_1 * value_2) % self.prime_mod + return polynome(coeff_poly_res) + + def evaluate(self, x): + # Using horner method + res = 0 + for i in range(len(self.coefs) - 1, -1, -1): + res = (res * x + self.coefs[i]) + return (res % self.prime_mod) diff --git a/reed_solomon.py b/reed_solomon.py index 0e7efb1..1183dc3 100644 --- a/reed_solomon.py +++ b/reed_solomon.py @@ -3,7 +3,7 @@ from polynome import * from inverse_mult import * from itertools import combinations -def get_possibilities(l: list): +def get_possibilities(l: list, index: int): # Transforme the list in list of tupples with their indexes for x, _ in enumerate(l): @@ -11,8 +11,8 @@ def get_possibilities(l: list): # ! la valeur 20 est hardcodée. Paramètre de fonction ?? - l_fixe = l[20:] # Liste contenant aucune erreur - l_posi = l[:20] # Liste contenant des erreurs, + l_fixe = l[index:] # Liste contenant aucune erreur + l_posi = l[:index] # Liste contenant des erreurs, p = list(combinations(l_posi, 2)) -- GitLab