Skip to content
Snippets Groups Projects
Commit 71d09cc2 authored by florian.burgener's avatar florian.burgener
Browse files

Refactoring

parent 077fa6fa
No related branches found
No related tags found
No related merge requests found
import math
import itertools import itertools
import math
def unicode_superscripts(number): def unicode_superscripts(number):
...@@ -27,7 +27,6 @@ class Polynomial: ...@@ -27,7 +27,6 @@ class Polynomial:
Raises: Raises:
TypeError: The type of the parameter "value" is not a tuple. TypeError: The type of the parameter "value" is not a tuple.
""" """
if not isinstance(value, tuple): if not isinstance(value, tuple):
raise TypeError('The "value" parameter is not of type tuple.') raise TypeError('The "value" parameter is not of type tuple.')
self.value = value self.value = value
...@@ -61,7 +60,7 @@ class Polynomial: ...@@ -61,7 +60,7 @@ class Polynomial:
b = list(other.value) b = list(other.value)
c = [] c = []
# itertools pad 0 to the lowest list. # Adds 0's to the list a if its length is less than that of b and vice versa.
for (ai, bi) in itertools.zip_longest(a, b, fillvalue=0): for (ai, bi) in itertools.zip_longest(a, b, fillvalue=0):
c.append(ai + bi) c.append(ai + bi)
return Polynomial(tuple(c)) return Polynomial(tuple(c))
...@@ -113,13 +112,13 @@ class Polynomial: ...@@ -113,13 +112,13 @@ class Polynomial:
for i, x in enumerate(reversed(self.value)): for i, x in enumerate(reversed(self.value)):
x = math.ceil(x) x = math.ceil(x)
if x == 0: if x == 0:
continue continue
if i != 0: if i != 0:
str_value += " + " if x >= 0 else " - " str_value += " + " if x >= 0 else " - "
if x != 1 or i == len(self.value) - 1: if x != 1 or i == len(self.value) - 1:
str_value += str(abs(x)) str_value += str(abs(x))
if len(self.value) - i - 1 >= 1: if len(self.value) - i - 1 >= 1:
str_value += "x" str_value += "x"
if len(self.value) - i - 1 >= 2: if len(self.value) - i - 1 >= 2:
...@@ -145,16 +144,15 @@ def get_bezout_coefficients(a, b): ...@@ -145,16 +144,15 @@ def get_bezout_coefficients(a, b):
while True: while True:
r.append(r[i - 2] % r[i - 1]) r.append(r[i - 2] % r[i - 1])
# Continue until the rest is equal to 0 # Continue until the rest is equal to 0
if r[i] == 0: if r[i] == 0:
break 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]) x.append(x[i - 2] - q[i] * x[i - 1])
y.append(y[i - 2] - q[i] * y[i - 1]) y.append(y[i - 2] - q[i] * y[i - 1])
i += 1 i += 1
return x[-1], y[-1] return x[-1], y[-1]
...@@ -223,27 +221,26 @@ def reed_solomon(points, data_length, last_error_index, prime_number): ...@@ -223,27 +221,26 @@ def reed_solomon(points, data_length, last_error_index, prime_number):
# 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 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]
# Pass the x value of each points through the lagrange polynomial # Pass the x value of each points through the lagrange polynomial
y = lagrange.evaluate_x(x) % prime_number y = lagrange.evaluate_x(x) % prime_number
# If the result is the same that the y value, then the point is correct # If the result is the same that the y value, then the point is correct
if y == p[1]: if y == p[1]:
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
# // = euclid division # // = euclid division
if nb_valid_points >= data_length + (len(points) - data_length) // 2: 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):
output += chr(lagrange.evaluate_x(i) % prime_number) output += chr(lagrange.evaluate_x(i) % prime_number)
return output return output
return None return None
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment