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 arutyunyan
from collections.abc import Iterable
import math
def quadratic_roots(a: int, b: int, c: int) -> Iterable[int | str]:
discrim = b * b - 4 * a * c
if discrim > 0:
root1 = (-b + math.sqrt(discrim)) / (2 * a)
root2 = (-b - math.sqrt(discrim)) / (2 * a)
return [root1, root2]
if not discrim:
root = -b / (2 * a)
return [root, root]
if discrim < 0:
return ["No real roots"]
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!")
Nov. 7, 2023
Comments: