Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Write Quadratic Equation by mortonfox
def quadr_equation(data: list[int]) -> str:
if len(data) == 2: data.append(data[1])
a, x1, x2 = data
b = -a * (x1 + x2)
c = a * x1 * x2
terms = []
for coef, pwr in zip([a, b, c], ['x**2', 'x', '']):
if not coef: continue
terms.append('-' if coef < 0 else '+')
coef = abs(coef)
term = str(coef) if coef > 1 or not pwr else ''
terms.append(term + ('*' if pwr and term else '') + pwr)
return ('-' if terms[0] == '-' else '') + ' '.join(terms[1:]) + ' = 0'
print("Example:")
print(quadr_equation([2, 4, 6]))
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!")
Sept. 9, 2022