Problem 109112 · medium · Level 01 Prerequisites & Setup

Real Roots of a Quadratic

functions · math module · conditionals

Write quadratic_roots(a, b, c) that returns the real solutions of a*x**2 + b*x + c = 0 as a list sorted from smallest to largest, each rounded to 2 decimal places. Return [] if there are no real roots and a one-element list if the two roots coincide.

Examples

Input:  a = 1, b = -3, c = 2
Output: [1.0, 2.0]

Input:  a = 1, b = 2, c = 1
Output: [-1.0]

Input:  a = 1, b = 0, c = 1
Output: []

Constraints

  • a != 0; all coefficients are ints or floats with absolute value at most 1000

Goals

  • Branch on the sign of the discriminant
  • Return a list whose length depends on the input
  • Round and sort floating-point results
Starting Python…