Skip to content

Commit 7ad5521

Browse files
MaximSmolskiygithub-actionspre-commit-ci[bot]
authored andcommitted
Reduce the complexity of linear_algebra/src/polynom_for_points.py (TheAlgorithms#8605)
* Reduce the complexity of linear_algebra/src/polynom_for_points.py * updating DIRECTORY.md * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix * Fix review issues --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 8681d06 commit 7ad5521

File tree

1 file changed

+19
-38
lines changed

1 file changed

+19
-38
lines changed

linear_algebra/src/polynom_for_points.py

+19-38
Original file line numberDiff line numberDiff line change
@@ -43,62 +43,43 @@ def points_to_polynomial(coordinates: list[list[int]]) -> str:
4343

4444
x = len(coordinates)
4545

46-
count_of_line = 0
47-
matrix: list[list[float]] = []
4846
# put the x and x to the power values in a matrix
49-
while count_of_line < x:
50-
count_in_line = 0
51-
a = coordinates[count_of_line][0]
52-
count_line: list[float] = []
53-
while count_in_line < x:
54-
count_line.append(a ** (x - (count_in_line + 1)))
55-
count_in_line += 1
56-
matrix.append(count_line)
57-
count_of_line += 1
47+
matrix: list[list[float]] = [
48+
[
49+
coordinates[count_of_line][0] ** (x - (count_in_line + 1))
50+
for count_in_line in range(x)
51+
]
52+
for count_of_line in range(x)
53+
]
5854

59-
count_of_line = 0
6055
# put the y values into a vector
61-
vector: list[float] = []
62-
while count_of_line < x:
63-
vector.append(coordinates[count_of_line][1])
64-
count_of_line += 1
56+
vector: list[float] = [coordinates[count_of_line][1] for count_of_line in range(x)]
6557

66-
count = 0
67-
68-
while count < x:
69-
zahlen = 0
70-
while zahlen < x:
71-
if count == zahlen:
72-
zahlen += 1
73-
if zahlen == x:
74-
break
75-
bruch = matrix[zahlen][count] / matrix[count][count]
58+
for count in range(x):
59+
for number in range(x):
60+
if count == number:
61+
continue
62+
fraction = matrix[number][count] / matrix[count][count]
7663
for counting_columns, item in enumerate(matrix[count]):
7764
# manipulating all the values in the matrix
78-
matrix[zahlen][counting_columns] -= item * bruch
65+
matrix[number][counting_columns] -= item * fraction
7966
# manipulating the values in the vector
80-
vector[zahlen] -= vector[count] * bruch
81-
zahlen += 1
82-
count += 1
67+
vector[number] -= vector[count] * fraction
8368

84-
count = 0
8569
# make solutions
86-
solution: list[str] = []
87-
while count < x:
88-
solution.append(str(vector[count] / matrix[count][count]))
89-
count += 1
70+
solution: list[str] = [
71+
str(vector[count] / matrix[count][count]) for count in range(x)
72+
]
9073

91-
count = 0
9274
solved = "f(x)="
9375

94-
while count < x:
76+
for count in range(x):
9577
remove_e: list[str] = solution[count].split("E")
9678
if len(remove_e) > 1:
9779
solution[count] = f"{remove_e[0]}*10^{remove_e[1]}"
9880
solved += f"x^{x - (count + 1)}*{solution[count]}"
9981
if count + 1 != x:
10082
solved += "+"
101-
count += 1
10283

10384
return solved
10485

0 commit comments

Comments
 (0)