Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Highschool way solution in Speedy category for Write Quadratic Equation by dig
def quadr_equation(data: list[int]) -> str:
if len (data) == 2 : data.append(data[-1])
a0 = data[0]
b0 = -a0*(data[1]+data[2])
c0 = a0*data[1]*data[2]
a = '' if a0==1 else '-' if a0==-1 else str(a0)+'*'
b = str(abs(b0))+'*'+'x' if b0 not in {0,1,-1} else 'x' if abs(b0) ==1 else ''
c = abs(c0) if c0 != 0 else ''
sign1, sign2 = '' if b0==0 else ' - ' if b0<0 else ' + ', '' if c0==0 else ' - ' if c0<0 else ' + '
return f"{a}x**2{sign1}{b}{sign2}{c} = 0"
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!")
Feb. 9, 2023
Comments: