@@ -24,96 +24,79 @@ def points_to_polynomial(coordinates: list[list[int]]) -> str:
24
24
>>> print(points_to_polynomial([[1, 5], [2, 2], [3, 9]]))
25
25
f(x)=x^2*5.0+x^1*-18.0+x^0*18.0
26
26
"""
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."
44
32
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."
50
39
51
40
x = len (coordinates )
52
41
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
66
54
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
73
61
74
- count = 0
62
+ count = 0
75
63
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 :
89
68
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
98
79
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
101
86
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)="
110
89
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
112
98
113
- elif check == 2 :
114
- return solved
115
- else :
116
- return "The program cannot work out a fitting polynomial."
99
+ return solved
117
100
118
101
119
102
if __name__ == "__main__" :
0 commit comments