In this example, you are expected to find the coefficients of the quadratic equation of the given two roots (x1 and x2) with a python function.

The Quadratic Equation looks like this ax^2 + bx + c = 0. Our mission is to find the coefficients of the equations which is a, b, and c. The return type from the function is a Vector containing coefficients of the equations in the order (a, b, c). Since there are infinitely many solutions to this problem, we fix a = 1.

Below is the method to find the return Vector.

quadratic(1,2) = (1, -3, 2)

This means (x-1) * (x-2) = 0; when we do the multiplication this becomes x^2 – 3x + 2 = 0.

def quadratic(x1, x2):
    a = 1
    b= a * -x2 + a * -x1
    c = -x1 * -x2
    return (a, b, c)

A very simple solution, we can also find the Vector even if a has a different value than one by creating another method with an extra ‘a’ parameter in the input.

quadratic(a, x1, x2)

This question is from CodeWars, which is a great place to learn how to code for free, besides Python, there are tons of other programming languages for you to select include Java and C#.

Please follow and like us:

Leave a Reply

Your email address will not be published. Required fields are marked *