Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Boring string manipulation solution in Clear category for Write Quadratic Equation by Phil15
def quadr_equation(data: list[int]) -> str:
try:
a, x1, x2 = data
except ValueError:
a, x1 = _, x2 = data
b = -a * (x1 + x2)
c = a * x1 * x2
ax = ('-' if a == -1 else '' if a == 1 else f'{a}*') + 'x**2'
bx = '' if not b else ('- ' if b == -1 else '+ ' if b == 1 else f'- {-b}*' if b < 0 else f'+ {b}*') + 'x'
cx = '' if not c else f'- {-c}' if c < 0 else f'+ {c}'
return " ".join(filter(None, (ax, bx, cx))) + ' = 0'
Aug. 21, 2022
Comments: