import math
def quadratic_roots(a: float, b: float, c: float) -> list[float]:
"""
Finds real roots (x ∈ ℝ ) of the quadratic equation:
ax² + bx + c = 0, where a, b, c, x ∈ ℝ
In case a is 0:
- It solves the correspondent linear equation (b != 0)
- Raises a ValueError otherwise (b == 0)
In case the equation only has imaginary solutions:
- Raises ValueError
Parameters:
a (float): Quadratic coefficient
b (float): Linear Coefficient
c (float): Constant term
Returns:
list[float]: Solutions
"""
roots = list()
if a == 0:
if b == 0:
raise ValueError("Invalid equation, 'a' and 'b' must be non-zero")
roots.append(-c / b)
return roots
discriminant = b**2 - 4 * a * c
if discriminant < 0:
raise ValueError("No real solutions (descriminant < 0)")
sqrt_d = math.sqrt(discriminant)
x1 = (-b + sqrt_d) / (2 * a)
x2 = (-b - sqrt_d) / (2 * a)
roots.append(x1)
roots.append(x2)
return rootsTesting
- Create a branch
testingin your gitlab repository - Create a new folder called
testing - Copy the code bellow into
testing/quadratic.pyand try to understand it. - Create a test module (
testing/test_quadratic.py) with test for thequadratic_rootsfunction- Use
pytest - Test as many scenarios as possible
- Use
- Write down your learnings from this process in
testing/learnings.md- With an explanation for each test function that you wrote!
- Open a merge request with Manuel Reis as reviewer
Tip
You might need to explore the pytest documentation 1
Code to be tested
Here’s a simple implementation of the quadratic formula2
Note
The structure of the repository should look like:
student-repo/
├── .git/
│ ...
│ ...
└── testing/
├── learnings.md
├── quadratic.py
└── test_quadratic.py