Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Quadratic Equation Roots by Khrystyna11
from collections.abc import Iterable
from typing import Union
def quadratic_roots(a: int, b: int, c: int) -> Iterable[Union[int | float] | str]:
# your code here
discriminant = b ** 2 - 4 * a * c
if discriminant < 0:
return ["No real roots"]
elif discriminant == 0:
root = -b / (2 * a)
return [root, root]
else:
sqrt_discriminant = discriminant ** 0.5
root_1 = (-b - sqrt_discriminant) / (2 * a)
root_2 = (-b + sqrt_discriminant) / (2 * a)
return [root_2, root_1]
print("Example:")
print(list(quadratic_roots(1, 2, 3)))
# These "asserts" are used for self-checking
assert list(quadratic_roots(1, -3, 2)) == [2, 1]
assert list(quadratic_roots(1, 0, -1)) == [1, -1]
assert list(quadratic_roots(1, 2, 1)) == [-1, -1]
assert list(quadratic_roots(1, 0, 1)) == ["No real roots"]
assert list(quadratic_roots(1, 4, 4)) == [-2, -2]
assert list(quadratic_roots(1, -5, 6)) == [3, 2]
assert list(quadratic_roots(1, -6, 9)) == [3, 3]
assert list(quadratic_roots(2, 2, 1)) == ["No real roots"]
assert list(quadratic_roots(2, -7, 6)) == [2, 1.5]
assert list(quadratic_roots(2, -3, 1)) == [1, 0.5]
print("The mission is done! Click 'Check Solution' to earn rewards!")
Sept. 6, 2024
Comments: