Skip to content

Add test for QuadraticEquation() #1107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 32 additions & 32 deletions maths/quadratic_equations_complex_numbers.py
Original file line number Diff line number Diff line change
@@ -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]:
"""
Given the numerical coefficients a, b and c,
prints the solutions for a quadratic equation, for a*x*x + b*x + c.

>>> QuadraticEquation(a=1, b=3, c=-4)
('1.0', '-4.0')
>>> QuadraticEquation(5, 6, 1)
('-0.2', '-1.0')
"""
Prints the solutions for a quadratic equation, given the numerical coefficients a, b and c,
for a*x*x + b*x + c.
Ex.: a = 1, b = 3, c = -4
Solution1 = 1 and Solution2 = -4
if a == 0:
raise ValueError("Coefficient 'a' must not be zero for quadratic equations.")
delta = b * b - 4 * a * c
if delta >= 0:
return str((-b + sqrt(delta)) / (2 * a)), str((-b - sqrt(delta)) / (2 * a))
"""
Delta = b*b - 4*a*c
if a != 0:
if Delta >= 0:
Solution1 = (-b + math.sqrt(Delta))/(2*a)
Solution2 = (-b - math.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
Solution1 = (- 2 + 4.0 *i)/2 and Solution2 = (- 2 + 4.0 *i)/ 10
"""
if b > 0:
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: (",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():
a = 5
b = 6
c = 1
Treats cases of Complexes Solutions(i = imaginary unit)
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:
return f"({snd} * i) / 2", f"({snd} * i) / {2 * a}"
b = -abs(b)
return f"({b}+{snd} * i) / 2", f"({b}+{snd} * i) / {2 * a}"


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


if __name__ == '__main__':
if __name__ == "__main__":
main()