-
Notifications
You must be signed in to change notification settings - Fork 0
/
computor
executable file
·186 lines (164 loc) · 6.23 KB
/
computor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python3
from sys import argv
from parser import parser, get_degree, is_term, is_number
from re import findall
allowed_symbols = ['X', '+', '-', '=', '^', '*', '.']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['+', '-', '=']
def get_term(term, sign='+'):
def get_number(x): return -float(x) if sign == '-' else float(x)
if is_number(number=term): # 5
return '', get_number(term)
elif is_term(term=term):
if term == 'X^0':
return '', get_number(1)
elif term == 'X' or term == 'X^1': # X
return 'X', get_number(1)
elif '*' in term: # 5 * X^2(0,1), 5 * X
term = term.split('*')
number = term[0]
if term[1] == 'X^0':
return '', get_number(number)
elif term[1] == 'X' or term[1] == 'X^1': # 5 * X
return 'X', get_number(number)
elif findall(r'X\^[0-2]', term[1]): # 5 * X^2
return 'X^' + term[-1].split('^')[-1], get_number(number)
elif findall(r'X\^[0-2]', term): # X^2
return 'X^' + term.split('^')[-1], get_number(1)
def get_data(equation: list):
i = 0
data = {"": [], 'X^0': [], 'X': [], 'X^2': []}
while i < len(equation):
if is_number(number=equation[i]) or is_term(term=equation[i]):
k, v = get_term(term=equation[i])
elif equation[i] == '+' or equation[i] == '-':
k, v = get_term(term=equation[i + 1], sign=equation[i])
i += 1
else:
break
data[k].append(v)
i += 1
return data
def change_operation(sign): return '+' if sign == '-' else '-'
def move_terms_to_left(equation):
equation = equation.replace(' * ', '*').split(' ')
right_side = []
left_side = []
for i in equation[::-1]:
if i == '=':
break
else:
right_side.append(i)
for i in equation:
if i == '=':
break
else:
left_side.append(i)
right_side = right_side[::-1]
tmp_right = [i for i in right_side]
result = []
i = 0
while i < len(right_side):
if is_number(number=right_side[i]) or is_term(term=right_side[i]):
# check if last item of left side is +/-.
if left_side[-1] == '-' or left_side[-1] == '+':
left_side.append(right_side[i])
else:
# check if previous item of equation is +/-.
if equation[equation.index(right_side[i]) - 1] == '+' or equation[equation.index(right_side[i]) - 1] == '-':
left_side.append(change_operation(sign=equation[equation.index(right_side[i]) - 1]))
else:
left_side.append('-')
left_side.append(right_side[i])
else:
# in case if item is +/-
left_side.append(change_operation(sign=right_side[i]))
left_side.append(right_side[i + 1])
i += 1
tmp_right.pop(0)
tmp_right.pop(0)
result= left_side + ['='] + (tmp_right if tmp_right else ['0'])
i += 1
return result
def reducer(data: dict):
data = {k: [sum(v)] for k, v in data.items()}
result = []
for k, v in data.items():
if v and v[0]:
number = int(v[0]) if v[0].is_integer() else v[0]
sign = '+' if number > 0 else '-'
number = -number if number < 0 else number
result.append(sign)
if k:
result.append((str(number) + '*' + k) if number != 1 else k)
else:
result.append((str(number) + k))
def is_solvable():
for i in result:
if 'X' in i:
return True
else:
return False
if result:
if is_solvable():
result += ['=', '0']
result.pop(0) if result[0] == '+' else result
print(f"Reduced form: {' '.join(result)}")
return result
else:
print("There no solutions.")
else:
print("All real numbers are solutions.")
return result
def solver(data: dict, degree: int):
def sqrt(number): return number ** (1/2)
a = data['X^2'][0] if data['X^2'] else 1
b = data['X'][0] if data['X'] else 0
c = data[''][0] if data[''] else 1
d = b * b - 4 * a * c
if degree == 2:
if d > 0:
print("Discriminant is strictly positive, the two solutions are:")
x1 = (-b + sqrt(d)) / (2 * a)
x2 = (-b - sqrt(d)) / (2 * a)
print(round(x1, 6), round(x2, 6))
elif d == 0:
print("Discriminant is equal to 0, the solution is:")
print(round(((-b + sqrt(d)) / (2 * a)), 6))
else:
print("Discriminant is strictly negative, the two solutions are:")
if b == 0:
x1 = sqrt(d) / (2 * a)
x2 = -sqrt(d) / (2 * a)
if round(x1.real) != 0:
x1 = str(round(x1.real, 6)) + ('+' if x1.imag > 0 else ' ') + str(round(x1.imag, 6)) + 'i'
x2 = str(round(x2.real, 6)) + ('-' if x2.imag > 0 else '') + str(round(x2.imag, 6)) + 'i'
else:
x1 = str(round(x1.imag, 6)) + 'i'
x2 = str(round(x2.imag, 6)) + 'i'
else:
comp = sqrt(d) / (2 * a)
x1 = round(-b / (2 * a), 6) + comp
x2 = round(-b / (2 * a), 6) - comp
x1 = str(round(x1.real, 6)) + ('+' if x1.imag > 0 else ' ') + str(round(x1.imag, 6)) + 'i'
x2 = str(round(x2.real, 6)) + ('-' if x2.imag > 0 else '') + str(round(x2.imag, 6)) + 'i'
print(x1, x2)
elif degree == 1:
c = 0 if not data[''] else c
print(f"The solution is:\n{round((-c / b), 6)}")
else:
print("WHAT?!")
def main():
if parser():
equation = argv[1]
equation = move_terms_to_left(equation=equation)
data = get_data(equation=equation)
data = reducer(data=data)
if data:
degree = get_degree(equation=' '.join(data))
data = get_data(equation=data)
solver(data=data, degree=degree)
else:
print("Wrong input!")
if __name__ == "__main__":
main()