Skip to content
Snippets Groups Projects
Commit fafe370e authored by gawen.ackerman's avatar gawen.ackerman :robot:
Browse files

refactoring

parent 48a4b7d4
No related branches found
No related tags found
No related merge requests found
...@@ -11,7 +11,8 @@ def unicode_superscripts(number): ...@@ -11,7 +11,8 @@ def unicode_superscripts(number):
Returns: Returns:
str: The unicode superscripts string. str: The unicode superscripts string.
""" """
exponent_dict = {"0": "", "1": "¹", "2": "²", "3": "³", "4": "", "5": "", "6": "", "7": "", "8": "", "9": ""} exponent_dict = {"0": "", "1": "¹", "2": "²", "3": "³",
"4": "", "5": "", "6": "", "7": "", "8": "", "9": ""}
return ("" if number < 0 else "") + "".join(exponent_dict[x] for x in str(abs(number))) return ("" if number < 0 else "") + "".join(exponent_dict[x] for x in str(abs(number)))
...@@ -59,12 +60,11 @@ class Polynomial: ...@@ -59,12 +60,11 @@ class Polynomial:
""" """
a = list(self.value) a = list(self.value)
b = list(other.value) b = list(other.value)
c = [] c = []
# itertools pad 0 to the lowest list
for (a, b) in itertools.zip_longest(a, b, fillvalue=0):
c.append(a + b)
# itertools pad 0 to the lowest list.
for (ai, bi) in itertools.zip_longest(a, b, fillvalue=0):
c.append(ai + bi)
return Polynomial(tuple(c)) return Polynomial(tuple(c))
def __mul__(self, other): def __mul__(self, other):
...@@ -83,6 +83,7 @@ class Polynomial: ...@@ -83,6 +83,7 @@ class Polynomial:
for i in range(len(a)): for i in range(len(a)):
for j in range(len(b)): for j in range(len(b)):
# Sum of the product of a[i] by b[j] at exponent i + j.
c[i + j] += a[i] * b[j] c[i + j] += a[i] * b[j]
return Polynomial(tuple(c)) return Polynomial(tuple(c))
...@@ -93,18 +94,14 @@ class Polynomial: ...@@ -93,18 +94,14 @@ class Polynomial:
other (int): The modulo to apply. other (int): The modulo to apply.
Returns: Returns:
Polynomial: The result of the modolu operation. Polynomial: The result of the modulo operation.
""" """
a = list(self.value) result = list(self.value)
result = [0] * len(a)
for i in range(len(a)): for i in range(len(result)):
result[i] = a[i] % other result[i] %= other
for i in reversed(range(len(result))): while result[-1] == 0 and len(result) > 1:
if result[i] == 0: result = result[:-1]
del result[i]
else:
break
return Polynomial(tuple(result)) return Polynomial(tuple(result))
def __str__(self): def __str__(self):
...@@ -145,25 +142,29 @@ def get_bezout_coefficients(a, b): ...@@ -145,25 +142,29 @@ def get_bezout_coefficients(a, b):
x = [1, 0] x = [1, 0]
y = [0, 1] y = [0, 1]
q = [0, 0] q = [0, 0]
i = 1 i = 2
while r[i] > 0: while True:
i += 1
r.append(r[i - 2] % r[i - 1]) r.append(r[i - 2] % r[i - 1])
# Continue until the rest is equal to 0
if r[i] == 0:
break
q.append(int(r[i - 2] / r[i - 1])) q.append(int(r[i - 2] / r[i - 1]))
x.append(x[i - 2] - q[i] * x[i - 1])
y.append(y[i - 2] - q[i] * y[i - 1])
i += 1
if r[i] > 0:
x.append(x[i - 2] - q[i] * x[i - 1])
y.append(y[i - 2] - q[i] * y[i - 1])
return x[-1], y[-1] return x[-1], y[-1]
def modular_inverse(a, n): def modular_inverse(a, n):
"""Compute the modular inverse of a number a modolu n. """Compute the modular inverse of a number a modulo n.
Args: Args:
a (int): The number to reverse. a (int): The number to reverse.
n (int): The modolu. n (int): The modulo.
Returns: Returns:
int: The reversed number. int: The reversed number.
...@@ -196,7 +197,7 @@ def compute_lagrange_polynomial(points, prime_number): ...@@ -196,7 +197,7 @@ def compute_lagrange_polynomial(points, prime_number):
for j, (xj, _) in enumerate(points): for j, (xj, _) in enumerate(points):
if j != i: if j != i:
Li_polynomial *= Polynomial((-xj, 1)) Li_polynomial *= Polynomial((-xj, 1))
Li_polynomial *= Polynomial((modular_inverse(xi - xj, prime_number),)) Li_polynomial *= Polynomial((modular_inverse(xi - xj, prime_number)))
Li_polynomial *= Polynomial((yi,)) Li_polynomial *= Polynomial((yi,))
L_polynomial += Li_polynomial L_polynomial += Li_polynomial
return L_polynomial % prime_number return L_polynomial % prime_number
...@@ -218,13 +219,13 @@ def reed_solomon(points, data_length, last_error_index, prime_number): ...@@ -218,13 +219,13 @@ def reed_solomon(points, data_length, last_error_index, prime_number):
# Parse each combination of points possible (exclude the correct points) # Parse each combination of points possible (exclude the correct points)
for x in itertools.combinations(points[: last_error_index + 1], combination_length): for x in itertools.combinations(points[: last_error_index + 1], combination_length):
nb_valid_points = 0
# Create a sublist of points with all corrects points and the current combination of points # Create a sublist of points with all corrects points and the current combination of points
sub_points = list(x) + points[last_error_index + 1 :] sub_points = list(x) + points[last_error_index + 1:]
# Create the lagrange polynomial with the sublist of points # Create the lagrange polynomial with the sublist of points
lagrange = compute_lagrange_polynomial(sub_points, prime_number) lagrange = compute_lagrange_polynomial(sub_points, prime_number)
nb_valid_points = 0
# Parse each points to verify if the polynomial is correct # Parse each points to verify if the polynomial is correct
for p in points: for p in points:
x = p[0] x = p[0]
...@@ -235,7 +236,8 @@ def reed_solomon(points, data_length, last_error_index, prime_number): ...@@ -235,7 +236,8 @@ def reed_solomon(points, data_length, last_error_index, prime_number):
nb_valid_points += 1 nb_valid_points += 1
# Verify if we have enough valid points, so it must be equal or higher than m + n points # Verify if we have enough valid points, so it must be equal or higher than m + n points
if nb_valid_points >= data_length + (len(points) - data_length) // 2: # // = euclid division # // = euclid division
if nb_valid_points >= data_length + (len(points) - data_length) // 2:
# Decode the message # Decode the message
output = "" output = ""
for i in range(data_length): for i in range(data_length):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment