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 math
def unicode_superscripts(number):
......@@ -27,7 +27,6 @@ class Polynomial:
Raises:
TypeError: The type of the parameter "value" is not a tuple.
"""
if not isinstance(value, tuple):
raise TypeError('The "value" parameter is not of type tuple.')
self.value = value
......@@ -61,7 +60,7 @@ class Polynomial:
b = list(other.value)
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):
c.append(ai + bi)
return Polynomial(tuple(c))
......@@ -113,13 +112,13 @@ class Polynomial:
for i, x in enumerate(reversed(self.value)):
x = math.ceil(x)
if x == 0:
continue
if i != 0:
str_value += " + " if x >= 0 else " - "
if x != 1 or i == len(self.value) - 1:
str_value += str(abs(x))
if len(self.value) - i - 1 >= 1:
str_value += "x"
if len(self.value) - i - 1 >= 2:
......@@ -145,16 +144,15 @@ def get_bezout_coefficients(a, b):
while True:
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]))
x.append(x[i - 2] - q[i] * x[i - 1])
y.append(y[i - 2] - q[i] * y[i - 1])
i += 1
return x[-1], y[-1]
......@@ -223,27 +221,26 @@ def reed_solomon(points, data_length, last_error_index, prime_number):
# Create the lagrange polynomial with the sublist of points
lagrange = compute_lagrange_polynomial(sub_points, prime_number)
nb_valid_points = 0
# Parse each points to verify if the polynomial is correct
for p in points:
x = p[0]
# Pass the x value of each points through the lagrange polynomial
y = lagrange.evaluate_x(x) % prime_number
# If the result is the same that the y value, then the point is correct
if y == p[1]:
nb_valid_points += 1
# Verify if we have enough valid points, so it must be equal or higher than m + n points
# // = euclid division
if nb_valid_points >= data_length + (len(points) - data_length) // 2:
# Decode the message
output = ""
for i in range(data_length):
output += chr(lagrange.evaluate_x(i) % prime_number)
return output
return None
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment