-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquadratic.py
149 lines (130 loc) · 4.91 KB
/
quadratic.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
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
'''
Created on Apr 24, 2019
@author: Bharddwaj Vemulapalli
username: bvemulap
I pledge my honor that I have abided by the Stevens Honor System.
'''
from math import sqrt
class QuadraticEquation():
def __init__(self,a,b,c):
try:
3/a
except:
raise ValueError("Coefficient 'a' cannot be 0 in a quadratic equation.")
self.__a = float(a)
self.__b = float(b)
self.__c = float(c)
@property
def a(self):
return self.__a
@property
def b(self):
return self.__b
@property
def c(self):
return self.__c
def discriminant(self):
return self.__b * self.__b - 4*self.__a*self.__c
def root1(self):
discriminant_val = self.discriminant()
if discriminant_val < float(0):
return None
return (-1*self.__b + sqrt(discriminant_val))/(2 * self.__a)
def root2(self):
discriminant_val = self.discriminant()
if discriminant_val < float(0):
return None
return (-1*self.__b - sqrt(discriminant_val))/(2 * self.__a)
def __str__(self):
sign_a = ''
sign_b = ''
sign_c = ''
a = self.__a
b = self.__b
c = self.__c
if a < 0:
sign_a = '-'
if a== -1.0:
a = ''
elif a > 0:
sign_a = ''
if a == 1.0:
a = ''
if b< 0:
sign_b = '-'
if b== -1.0:
b = ''
else:
b = str(self.__b)[1:]
elif b > 0:
sign_b = '+'
if b == 1.0:
b = ''
if c< 0:
sign_c = '-'
c = str(self.__c)[1:]
elif c> 0:
sign_c = '+'
elif c == 0:
c = ''
if self.__a != 0 and self.__b != 0 and self.__c!= 0:
return f'{sign_a}{a}x^2 {sign_b} {b}x {sign_c} {c} = 0'
elif self.__a != 0 and self.__b == 0 and self.__c!= 0:
return f'{sign_a}{a}x^2 {sign_c} {c} = 0'
elif self.__a == 0 and self.__b != 0 and self.__c!= 0:
return f'{sign_b} {b}x {sign_c} {c} = 0'
elif self.__a == 0 and self.__c == 0:
return f'{sign_b} {b}x = 0'
elif self.__b == 0 and self.__c == 0:
return f'{sign_a}{a}x^2 = 0'
if self.__a != 0 and self.__b != 0 and self.__c== 0:
return f'{sign_a}{a}x^2 {sign_b} {b}x = 0'
'''
too much work:
if self.__b == 0 and self.__c == 0:
return f'{self.__a}x^2 = 0'
elif self.__a == 1 and self.__b == 0 and self.__c == 0 :
return 'x^2 = 0'
elif self.__a == 1 and self.__b == 0:
return f'x^2 + {self.__c} = 0'
elif self.__a == 1 and self.__c == 0:
return f'x^2 + {self.__b}x = 0'
elif self.__a == 1 and self.__b == 1 and self.__c == 0:
return 'x^2 + x = 0'
elif self.__a == 1 and self.__b == 1 :
return f'x^2 + x + {self.__c} = 0'
elif self.__c == 0:
return f'{self.__a}x^2 + {self.__b}x = 0'
elif self.__b == 0:
return f'{self.__a}x^2 + {self.__c} = 0'
elif self.__a == 1:
return f'x^2 + {self.__b}x + {self.__c} = 0'
elif self.__a == -1 and self.__b == 0 and self.__c == 0 :
return '-x^2 = 0'
elif self.__a == -1 and self.__b == 0:
return f'-x^2 + {self.__c} = 0'
elif self.__a == -1 and self.__c == 0:
return f'-x^2 + {self.__b}x = 0'
elif self.__a == -1 and self.__b == -1 and self.__c == 0:
return '-x^2 - x = 0'
elif self.__a == -1 and self.__b == -1 :
return f'-x^2 - x + {self.__c} = 0'
elif self.__a == -1:
return f'-x^2 + {self.__b}x + {self.__c} = 0'
inefficient and doesn't work:
something = [f'{self.__a}x^2',f'{self.__b}x']
something = list(map(lambda x: '' if float(x[:x.index('x')]) == 0 else x[x.index('x'):] if float(x[:x.index('x')]) == 1.0 \
else '-' + x[x.index('x'):] if x[0] == '-'else x,something))
if self.__c !=0 and len(something[1]) > 0:
x = something[1]
if x[0] != '-' and self.__c < 0 :
return something[0] + ' + ' + something[1] + ' - ' + str(self.__c)[1:] + ' = 0'
elif x[0] != '-'and self.__c > 0 :
return something[0] + ' + ' + something[1] + ' + ' + str(self.__c) + ' = 0'
elif x[0] == '-' and self.__c < 0 :
return something[0] + ' - ' + something[1][1:] + ' - ' + str(self.__c)[1:] + ' = 0'
elif x[0] == '-'and self.__c > 0 :
return something[0] + ' - ' + something[1][1:] + ' + ' + str(self.__c) + ' = 0'
elif :
return something[0] + something[1] + ' = 0'
'''