-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.py
56 lines (51 loc) · 1.63 KB
/
calc.py
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
import sys
from parse import ParseArg
class Calc(ParseArg):
def solve(self):
self.clean()
self.polynomial()
if self.pol == 1:
self.linear()
elif self.pol == 2:
self.quadratic()
def clean(self):
temp = {}
for x in self.equ:
if self.equ[x] != 0:
temp[x] = self.equ[x]
if bool(temp) == False:
sys.exit('Each real number is a solution!')
self.s_equ = dict(sorted(temp.items(), key=lambda item: item[0]))
self.pol = [*self.s_equ][-1]
i = 0
while i < self.pol:
if i in self.s_equ.keys():
pass
else:
self.s_equ[i] = 0
i += 1
def polynomial(self):
print ('Polynomial degree: %d' % int(self.pol))
if self.pol > 2:
sys.exit('The polynomial degree is strictly greater than 2, I can\'t solve.')
if self.pol == 0:
sys.exit('Invalid equation!')
def linear(self):
result = -(self.s_equ[0]) / self.s_equ[1]
print ('The solution is:')
print (int(result) if result == int(result) else round(result, 6))
def quadratic(self):
result = [0] * 2
if self.s_equ[1] ** 2 < (4 * self.s_equ[2] * self.s_equ[0]):
sys.exit('Discriminant is negative. The polynominal doesn\'t have real solutions.')
result[0] = (-self.s_equ[1] - \
(self.s_equ[1] ** 2 - (4 * self.s_equ[2] * self.s_equ[0])) ** 0.5) \
/ (2 * self.s_equ[2])
result[1] = (-self.s_equ[1] + \
(self.s_equ[1] ** 2 - (4 * self.s_equ[2] * self.s_equ[0])) ** 0.5) \
/ (2 * self.s_equ[2])
print('Discriminant is strictly positive, the two solutions are:')
print (int(result[0]) if result[0] == int(result[0]) \
else round(result[0], 6))
print (int(result[1]) if result[1] == int(result[1]) \
else round(result[1], 6))