Решение квадратного уравнения | Python

Необходимо решить квадратное уравнение

Формат ввода Вводится 3 вещественных числа a, b, c — коэффициенты уравнения вида: ax^2+bx+c=0ax

Формат вывода Если у уравнения нет решений — следует вывести «No solution». Если корней конечное число — их нужно вывести через пробел в порядке возрастания с точностью до сотых. Если корней неограниченное число — следует вывести «Infinite solutions».

Примечание Обратите внимание, что ограничения на значения коэффициентов отсутствуют.

a = float(input())
b = float(input())
c = float(input())
x1 = float(0.00)
x2 = float(0.00)
if a == 0 and b == 0 and c == 0:
    print("Infinite solutions")
elif a == 0 and b != 0 and c != 0:
    x1 = -(c / b)
    print("%.2f" % x1)
elif a == 0 and b == 0 and c != 0:
    print("No solution")  # no sol
elif a == 0 and b != 0 and c == 0:
    x1 = 0
    print("%.2f" % x1)
elif b == 0 and c == 0 and a != 0:
    x1 = 0
    print("%.2f" % x1)
elif c == 0 and b != 0 and a != 0:
    x1 = 0
    x2 = -(b / a)
    print(min("%.2f" % x1, "%.2f" % x2), max("%.2f" % x1, "%.2f" % x2), sep=' ')
elif b == 0 and a != 0 and c != 0:
    if (-(c / a)) < 0:
        print("No solution")
    else:
        x1 = (-c / a) ** 0.5
        x2 = -((-c / a) ** 0.5)
        print(min("%.2f" % x1, "%.2f" % x2), max("%.2f" % x1, "%.2f" % x2), sep=' ')
elif a != 0 and b != 0 and c != 0:
    disc = (b ** 2) - (4 * a * c)  # discriminant
    if disc < 0:
        print("No solution")
    elif disc == 0:
        x1 = (-b / (2 * a))
        print("%.2f" % x1)
    elif disc > 0:
        x1 = (-b - (disc ** 0.5)) / (2 * a)
        x2 = (-b + (disc ** 0.5)) / (2 * a)
        print(min("%.2f" % x1, "%.2f" % x2), max("%.2f" % x1, "%.2f" % x2), sep=' ')

Итог: 13 тестов код проходит, а на 14-ом ловлю ошибку "Wrong answer(WA)"

UPD.Итог2.0:

Примитивная ликвидация %f форматтера спасла ситуацию и код прошёл все тесты, после чего на пол экрана проявилась табличка с зелёным шрифтом "Решена полностью". Спасибо всем причастным к решению моей проблемы как советами, так и комментариями, задачу требовалось решить зная лишь 3 типа данных, условные и логические операторы - задача выполнена. Рабочий код ниже:

a = float(input())
b = float(input())
c = float(input())
x1 = float(0.00)
x2 = float(0.00)
if a == 0 and b == 0 and c == 0:
    print("Infinite solutions")
elif a == 0 and b != 0 and c != 0:
    x1 = -(c / b)
    print(x1)
elif a == 0 and b == 0 and c != 0:
    print("No solution")  # no sol
elif a == 0 and b != 0 and c == 0:
    x1 = 0
    print(x1)
elif b == 0 and c == 0 and a != 0:
    x1 = 0
    print(x1)
elif c == 0 and b != 0 and a != 0:
    x1 = 0
    x2 = -(b / a)
    print(min(x1, x2), max(x1, x2), sep=' ')
elif b == 0 and a != 0 and c != 0:
    if (-(c / a)) < 0:
        print("No solution")
    else:
        x1 = (-c / a) ** 0.5
        x2 = -((-c / a) ** 0.5)
        print(min(x1, x2), max(x1, x2), sep=' ')
elif a != 0 and b != 0 and c != 0:
    disc = (b ** 2) - (4 * a * c)  # discriminant
    if disc < 0:
        print("No solution")
    elif disc == 0:
        x1 = (-b / (2 * a))
        print("%.2f" % x1)
    elif disc > 0:
        x1 = (-b - (disc ** 0.5)) / (2 * a)
        x2 = (-b + (disc ** 0.5)) / (2 * a)
        print(min(x1, x2), max(x1, x2), sep=' ')

Укороченный код:

a = float(input())
b = float(input())
c = float(input())
x1 = float(0.00)
x2 = float(0.00)
if a == 0 and b == 0 and c == 0:
    print("Infinite solutions")
elif a == 0 and b != 0 and c != 0:
    x1 = -(c / b)   
    print(x1)
elif a == 0 and b == 0 and c != 0:
    print("No solution")  # no sol
elif a == 0 and b != 0 and c == 0:
    x1 = 0
    print(x1)
else:
    disc = (b ** 2) - (4 * a * c)  # discriminant
    if disc == 0:
        x1 = (-b) / (2 * a)
        print(x1)
    elif disc > 0:
        x1 = (-b - (disc ** 0.5)) / (2 * a)
        x2 = (-b + (disc ** 0.5)) / (2 * a)
        print(min(x1, x2), max(x1, x2), sep=' ')
    elif disc < 0:
        print("No solution")


Ответы (3 шт):

Автор решения: MIkhail

вот общее решение квадратного уравнения

from cmath import *


def linear(a, b):
    solutions = set()
    if a == 0 and b == 0:
        solutions.add(True)

    if a == 0 and b != 0:
        solutions.add(False)

    if a != 0:
        solutions.add(-b / a)
    return solutions


def quadratic(a, b, c):
    solutions = set()
    if a != 0:
        D = b ** 2 - 4 * a * c
        x1 = (-b + sqrt(D)) / (2 * a)
        x2 = (-b - sqrt(D)) / (2 * a)
        solutions.update({x1, x2})
    else:
        solutions.update(linear(b, c))
    return solutions


print('ax^2+bx+c=0')
a, b, c = map(complex, input().split())

print(quadratic(a, b, c))
→ Ссылка
Автор решения: Fire-Fox

Тоже яндекс handbook?

a = float(input())
b = float(input())
c = float(input())
if a == 0:
    if b == 0:
        if c == 0:
            print("Infinite solutions")
        else:
            print("No solution")
    else:
        print(f"{-c / b:.2f}")
else:
    d = b ** 2 - 4 * a * c
    if d < 0:
        print("No solution")
    else:
        x1 = (-b - d ** 0.5) / (2 * a)
        x2 = (-b + d ** 0.5) / (2 * a)
        print(f"{x1:.2f}" if x1 == x2 else f"{min(x1, x2):.2f} {max(x1, x2):.2f}")
→ Ссылка
Автор решения: AlexOmelyanenko

Решение задачи на полный балл:

a = float(input())
b = float(input())
c = float(input())

if (a == 0 and b == 0 and c == 0):
    print("Infinite solutions")

if (a == 0 and b == 0):
    print("No solution")
if a != 0:
    D = b ** 2 - 4 * a * c
    if b == 0 and c == 0:
        print(0)
    elif b == 0:
        if c > 0:
            print(0)
        else:
            x1 = (-c / a) ** 0.5
            x2 = -((-c / a) ** 0.5)
            mas = [round(x1, 2), round(x2, 2)]
            print(*sorted(mas))
    elif D < 0:
        print("No solution")
    elif D == 0:
        x = -b / 2 * a
        print(round(x, 2))
    elif D > 0:
        x1 = (-1 * b + D ** 0.5) / (2 * a)
        x2 = (-1 * b - D ** 0.5) / (2 * a)
        mas = [round(x1, 2), round(x2, 2)]
        print(*sorted(mas))
        
if a == 0 and b != 0:
    if c == 0:
        print(0)
    else:
        print(round(-c / b, 2))
→ Ссылка