Skip to content

Commit db5215f

Browse files
MaximSmolskiygithub-actionscclauss
authored
Reduce the complexity of linear_algebra/src/polynom_for_points.py (#7948)
* updating DIRECTORY.md * updating DIRECTORY.md * updating DIRECTORY.md * Lower the --max-complexity threshold in the file .flake8 * Reduce the complexity of linear_algebra/src/polynom_for_points.py * Update linear_algebra/src/polynom_for_points.py Co-authored-by: Christian Clauss <[email protected]> * Update linear_algebra/src/polynom_for_points.py Co-authored-by: Christian Clauss <[email protected]> * Fix Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent 45b3383 commit db5215f

File tree

2 files changed

+62
-79
lines changed

2 files changed

+62
-79
lines changed

Diff for: .flake8

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[flake8]
22
max-line-length = 88
33
# max-complexity should be 10
4-
max-complexity = 23
4+
max-complexity = 21
55
extend-ignore =
66
# Formatting style for `black`
77
E203 # Whitespace before ':'

Diff for: linear_algebra/src/polynom_for_points.py

+61-78
Original file line numberDiff line numberDiff line change
@@ -24,96 +24,79 @@ def points_to_polynomial(coordinates: list[list[int]]) -> str:
2424
>>> print(points_to_polynomial([[1, 5], [2, 2], [3, 9]]))
2525
f(x)=x^2*5.0+x^1*-18.0+x^0*18.0
2626
"""
27-
try:
28-
check = 1
29-
more_check = 0
30-
d = coordinates[0][0]
31-
for j in range(len(coordinates)):
32-
if j == 0:
33-
continue
34-
if d == coordinates[j][0]:
35-
more_check += 1
36-
solved = "x=" + str(coordinates[j][0])
37-
if more_check == len(coordinates) - 1:
38-
check = 2
39-
break
40-
elif more_check > 0 and more_check != len(coordinates) - 1:
41-
check = 3
42-
else:
43-
check = 1
27+
if len(coordinates) == 0 or not all(len(pair) == 2 for pair in coordinates):
28+
return "The program cannot work out a fitting polynomial."
29+
30+
if len({tuple(pair) for pair in coordinates}) != len(coordinates):
31+
return "The program cannot work out a fitting polynomial."
4432

45-
if len(coordinates) == 1 and coordinates[0][0] == 0:
46-
check = 2
47-
solved = "x=0"
48-
except Exception:
49-
check = 3
33+
set_x = {x for x, _ in coordinates}
34+
if len(set_x) == 1:
35+
return f"x={coordinates[0][0]}"
36+
37+
if len(set_x) != len(coordinates):
38+
return "The program cannot work out a fitting polynomial."
5039

5140
x = len(coordinates)
5241

53-
if check == 1:
54-
count_of_line = 0
55-
matrix: list[list[float]] = []
56-
# put the x and x to the power values in a matrix
57-
while count_of_line < x:
58-
count_in_line = 0
59-
a = coordinates[count_of_line][0]
60-
count_line: list[float] = []
61-
while count_in_line < x:
62-
count_line.append(a ** (x - (count_in_line + 1)))
63-
count_in_line += 1
64-
matrix.append(count_line)
65-
count_of_line += 1
42+
count_of_line = 0
43+
matrix: list[list[float]] = []
44+
# put the x and x to the power values in a matrix
45+
while count_of_line < x:
46+
count_in_line = 0
47+
a = coordinates[count_of_line][0]
48+
count_line: list[float] = []
49+
while count_in_line < x:
50+
count_line.append(a ** (x - (count_in_line + 1)))
51+
count_in_line += 1
52+
matrix.append(count_line)
53+
count_of_line += 1
6654

67-
count_of_line = 0
68-
# put the y values into a vector
69-
vector: list[float] = []
70-
while count_of_line < x:
71-
vector.append(coordinates[count_of_line][1])
72-
count_of_line += 1
55+
count_of_line = 0
56+
# put the y values into a vector
57+
vector: list[float] = []
58+
while count_of_line < x:
59+
vector.append(coordinates[count_of_line][1])
60+
count_of_line += 1
7361

74-
count = 0
62+
count = 0
7563

76-
while count < x:
77-
zahlen = 0
78-
while zahlen < x:
79-
if count == zahlen:
80-
zahlen += 1
81-
if zahlen == x:
82-
break
83-
bruch = matrix[zahlen][count] / matrix[count][count]
84-
for counting_columns, item in enumerate(matrix[count]):
85-
# manipulating all the values in the matrix
86-
matrix[zahlen][counting_columns] -= item * bruch
87-
# manipulating the values in the vector
88-
vector[zahlen] -= vector[count] * bruch
64+
while count < x:
65+
zahlen = 0
66+
while zahlen < x:
67+
if count == zahlen:
8968
zahlen += 1
90-
count += 1
91-
92-
count = 0
93-
# make solutions
94-
solution: list[str] = []
95-
while count < x:
96-
solution.append(str(vector[count] / matrix[count][count]))
97-
count += 1
69+
if zahlen == x:
70+
break
71+
bruch = matrix[zahlen][count] / matrix[count][count]
72+
for counting_columns, item in enumerate(matrix[count]):
73+
# manipulating all the values in the matrix
74+
matrix[zahlen][counting_columns] -= item * bruch
75+
# manipulating the values in the vector
76+
vector[zahlen] -= vector[count] * bruch
77+
zahlen += 1
78+
count += 1
9879

99-
count = 0
100-
solved = "f(x)="
80+
count = 0
81+
# make solutions
82+
solution: list[str] = []
83+
while count < x:
84+
solution.append(str(vector[count] / matrix[count][count]))
85+
count += 1
10186

102-
while count < x:
103-
remove_e: list[str] = solution[count].split("E")
104-
if len(remove_e) > 1:
105-
solution[count] = remove_e[0] + "*10^" + remove_e[1]
106-
solved += "x^" + str(x - (count + 1)) + "*" + str(solution[count])
107-
if count + 1 != x:
108-
solved += "+"
109-
count += 1
87+
count = 0
88+
solved = "f(x)="
11089

111-
return solved
90+
while count < x:
91+
remove_e: list[str] = solution[count].split("E")
92+
if len(remove_e) > 1:
93+
solution[count] = f"{remove_e[0]}*10^{remove_e[1]}"
94+
solved += f"x^{x - (count + 1)}*{solution[count]}"
95+
if count + 1 != x:
96+
solved += "+"
97+
count += 1
11298

113-
elif check == 2:
114-
return solved
115-
else:
116-
return "The program cannot work out a fitting polynomial."
99+
return solved
117100

118101

119102
if __name__ == "__main__":

0 commit comments

Comments
 (0)