Add test for QuadraticEquation() (#1107)

This commit is contained in:
Christian Clauss 2019-08-06 21:32:27 +02:00 committed by GitHub
parent 7b5a18453b
commit 7cf3db1843
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,39 +1,39 @@
import math from math import sqrt
from typing import Tuple
def QuadraticEquation(a,b,c):
def QuadraticEquation(a: int, b: int, c: int) -> Tuple[str, str]:
""" """
Prints the solutions for a quadratic equation, given the numerical coefficients a, b and c, Given the numerical coefficients a, b and c,
for a*x*x + b*x + c. prints the solutions for a quadratic equation, for a*x*x + b*x + c.
Ex.: a = 1, b = 3, c = -4
Solution1 = 1 and Solution2 = -4 >>> QuadraticEquation(a=1, b=3, c=-4)
('1.0', '-4.0')
>>> QuadraticEquation(5, 6, 1)
('-0.2', '-1.0')
""" """
Delta = b*b - 4*a*c if a == 0:
if a != 0: raise ValueError("Coefficient 'a' must not be zero for quadratic equations.")
if Delta >= 0: delta = b * b - 4 * a * c
Solution1 = (-b + math.sqrt(Delta))/(2*a) if delta >= 0:
Solution2 = (-b - math.sqrt(Delta))/(2*a) return str((-b + sqrt(delta)) / (2 * a)), str((-b - sqrt(delta)) / (2 * a))
print("The equation solutions are: ", Solution1," and ", Solution2) """
else: Treats cases of Complexes Solutions(i = imaginary unit)
""" Ex.: a = 5, b = 2, c = 1
Treats cases of Complexes Solutions(i = imaginary unit) Solution1 = (- 2 + 4.0 *i)/2 and Solution2 = (- 2 + 4.0 *i)/ 10
Ex.: a = 5, b = 2, c = 1 """
Solution1 = (- 2 + 4.0 *i)/2 and Solution2 = (- 2 + 4.0 *i)/ 10 snd = sqrt(-delta)
""" if b == 0:
if b > 0: return f"({snd} * i) / 2", f"({snd} * i) / {2 * a}"
print("The equation solutions are: (-",b,"+",math.sqrt(-Delta),"*i)/2 and (-",b,"+",math.sqrt(-Delta),"*i)/", 2*a) b = -abs(b)
if b < 0: return f"({b}+{snd} * i) / 2", f"({b}+{snd} * i) / {2 * a}"
print("The equation solutions are: (",b,"+",math.sqrt(-Delta),"*i)/2 and (",b,"+",math.sqrt(-Delta),"*i/",2*a)
if b == 0:
print("The equation solutions are: (",math.sqrt(-Delta),"*i)/2 and ",math.sqrt(-Delta),"*i)/", 2*a)
else:
print("Error. Please, coeficient 'a' must not be zero for quadratic equations.")
def main(): def main():
a = 5 solutions = QuadraticEquation(a=5, b=6, c=1)
b = 6 print("The equation solutions are: {} and {}".format(*solutions))
c = 1 # The equation solutions are: -0.2 and -1.0
QuadraticEquation(a,b,c) # The equation solutions are: -0.2 and -1.0
if __name__ == '__main__': if __name__ == "__main__":
main() main()