Skip to content

Commit 7cf3db1

Browse files
authored
Add test for QuadraticEquation() (TheAlgorithms#1107)
1 parent 7b5a184 commit 7cf3db1

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed
+32-32
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
import math
1+
from math import sqrt
2+
from typing import Tuple
23

3-
def QuadraticEquation(a,b,c):
4+
5+
def QuadraticEquation(a: int, b: int, c: int) -> Tuple[str, str]:
6+
"""
7+
Given the numerical coefficients a, b and c,
8+
prints the solutions for a quadratic equation, for a*x*x + b*x + c.
9+
10+
>>> QuadraticEquation(a=1, b=3, c=-4)
11+
('1.0', '-4.0')
12+
>>> QuadraticEquation(5, 6, 1)
13+
('-0.2', '-1.0')
414
"""
5-
Prints the solutions for a quadratic equation, given the numerical coefficients a, b and c,
6-
for a*x*x + b*x + c.
7-
Ex.: a = 1, b = 3, c = -4
8-
Solution1 = 1 and Solution2 = -4
15+
if a == 0:
16+
raise ValueError("Coefficient 'a' must not be zero for quadratic equations.")
17+
delta = b * b - 4 * a * c
18+
if delta >= 0:
19+
return str((-b + sqrt(delta)) / (2 * a)), str((-b - sqrt(delta)) / (2 * a))
920
"""
10-
Delta = b*b - 4*a*c
11-
if a != 0:
12-
if Delta >= 0:
13-
Solution1 = (-b + math.sqrt(Delta))/(2*a)
14-
Solution2 = (-b - math.sqrt(Delta))/(2*a)
15-
print("The equation solutions are: ", Solution1," and ", Solution2)
16-
else:
17-
"""
18-
Treats cases of Complexes Solutions(i = imaginary unit)
19-
Ex.: a = 5, b = 2, c = 1
20-
Solution1 = (- 2 + 4.0 *i)/2 and Solution2 = (- 2 + 4.0 *i)/ 10
21-
"""
22-
if b > 0:
23-
print("The equation solutions are: (-",b,"+",math.sqrt(-Delta),"*i)/2 and (-",b,"+",math.sqrt(-Delta),"*i)/", 2*a)
24-
if b < 0:
25-
print("The equation solutions are: (",b,"+",math.sqrt(-Delta),"*i)/2 and (",b,"+",math.sqrt(-Delta),"*i/",2*a)
26-
if b == 0:
27-
print("The equation solutions are: (",math.sqrt(-Delta),"*i)/2 and ",math.sqrt(-Delta),"*i)/", 2*a)
28-
else:
29-
print("Error. Please, coeficient 'a' must not be zero for quadratic equations.")
30-
def main():
31-
a = 5
32-
b = 6
33-
c = 1
21+
Treats cases of Complexes Solutions(i = imaginary unit)
22+
Ex.: a = 5, b = 2, c = 1
23+
Solution1 = (- 2 + 4.0 *i)/2 and Solution2 = (- 2 + 4.0 *i)/ 10
24+
"""
25+
snd = sqrt(-delta)
26+
if b == 0:
27+
return f"({snd} * i) / 2", f"({snd} * i) / {2 * a}"
28+
b = -abs(b)
29+
return f"({b}+{snd} * i) / 2", f"({b}+{snd} * i) / {2 * a}"
30+
3431

35-
QuadraticEquation(a,b,c) # The equation solutions are: -0.2 and -1.0
32+
def main():
33+
solutions = QuadraticEquation(a=5, b=6, c=1)
34+
print("The equation solutions are: {} and {}".format(*solutions))
35+
# The equation solutions are: -0.2 and -1.0
3636

3737

38-
if __name__ == '__main__':
38+
if __name__ == "__main__":
3939
main()

0 commit comments

Comments
 (0)