Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
If else solution in Clear category for Write Quadratic Equation by Molot
def quadr_equation(data: list[int]) -> str:
if len(data) == 3:
a, x1, x2 = data
else:
a, x1 = data
x2 = x1
c1 = f'{a}*x**2'
c2 = f' {["-","+"][-a*(x1 + x2) > 0]} {abs(a*(x1 + x2))}*x' if a*(x1 + x2) else ''
c3 = f' {["-","+"][a*x1*x2 > 0]} {abs(a*x1*x2)}' if a*x1*x2 else ''
return (c1 + c2 + c3 + ' = 0').replace('1*x', 'x')
print("Example:")
print(quadr_equation([2, 4, 6]))
# These "asserts" are used for self-checking
assert quadr_equation([2, 4, 6]) == "2*x**2 - 20*x + 48 = 0"
assert quadr_equation([-2, 4, 6]) == "-2*x**2 + 20*x - 48 = 0"
assert quadr_equation([2, 4, -4]) == "2*x**2 - 32 = 0"
assert quadr_equation([2, 4, 0]) == "2*x**2 - 8*x = 0"
assert quadr_equation([2, 0]) == "2*x**2 = 0"
assert quadr_equation([2, 4]) == "2*x**2 - 16*x + 32 = 0"
print("The mission is done! Click 'Check Solution' to earn rewards!")
Dec. 6, 2022
Comments: