All problems
Extrema of Quadratic on Interval
Problem
Find the minimum and maximum values of f(x) = a*x^2 + b*x + c on the closed interval [lo, hi].
Check the endpoints and the vertex x = -b / (2*a) when the vertex lies inside the interval, then return [min_value, max_value]. For a == 0 the function is linear.
Hint
An extreme value on a closed interval occurs at an endpoint or at an interior critical point.
Test cases
case 1
in: [1,0,0,-1,2]
expected: [0,4]
case 2
in: [-1,2,0,0,3]
expected: [-3,1]
case 3
in: [0,2,1,-1,1]
expected: [-1,3]
case 4
in: [1,-4,4,0,1]
expected: [1,4]
case 5
in: [2,0,-8,-2,1]
expected: [-8,0]
Starter codePython
def quadratic_extrema(a, b, c, lo, hi):
# Return [min_value, max_value]
# Your code here
pass5 test cases · run your code against them in the editor