Challenge - 5 Problems
Polynomial Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of polynomial addition using np.polyadd
What is the output of this code that adds two polynomials using
np.polyadd?NumPy
import numpy as np p1 = np.array([1, -3, 2]) # x^2 - 3x + 2 p2 = np.array([0, 4, -1]) # 4x - 1 result = np.polyadd(p1, p2) print(result)
Attempts:
2 left
💡 Hint
Remember that np.polyadd adds coefficients of the same degree terms.
✗ Incorrect
The polynomials are p1 = x^2 - 3x + 2 and p2 = 4x - 1. Adding them gives x^2 + ( -3 + 4 )x + (2 - 1) = x^2 + x + 1, so coefficients are [1, 1, 1].
❓ data_output
intermediate2:00remaining
Degree of polynomial from roots using np.poly
Given roots [-1, 0, 2], what is the degree and coefficients of the polynomial generated by
np.poly?NumPy
import numpy as np roots = [-1, 0, 2] coeffs = np.poly(roots) degree = len(coeffs) - 1 print(degree) print(coeffs)
Attempts:
2 left
💡 Hint
np.poly returns coefficients of polynomial with given roots, highest degree first.
✗ Incorrect
The polynomial with roots -1, 0, 2 is (x+1)(x)(x-2) = x^3 - x^2 - 2x. Coefficients are [1, -1, -2, 0]. Degree is 3.
❓ visualization
advanced3:00remaining
Plot polynomial and its derivative
Which option correctly plots the polynomial
p(x) = 2x^3 - 3x^2 + x - 5 and its derivative using np.polyder?NumPy
import numpy as np import matplotlib.pyplot as plt p = np.array([2, -3, 1, -5]) p_der = np.polyder(p) x = np.linspace(-2, 3, 100) y = np.polyval(p, x) y_der = np.polyval(p_der, x) plt.plot(x, y, label='Polynomial') plt.plot(x, y_der, label='Derivative') plt.legend() plt.show()
Attempts:
2 left
💡 Hint
np.polyder returns coefficients of the derivative polynomial.
✗ Incorrect
Option A correctly uses np.polyder and plots both polynomial and its derivative with labels.
🔧 Debug
advanced2:00remaining
Identify error in polynomial multiplication code
What error does this code raise when multiplying two polynomials using
np.polymul incorrectly?NumPy
import numpy as np p1 = [1, 2] p2 = (3, 4) result = np.polymul(p1, p2) print(result)
Attempts:
2 left
💡 Hint
Check the data types of the polynomial coefficient inputs.
✗ Incorrect
p2 is a tuple, but np.polymul expects array-like (list or np.array). Passing a tuple causes TypeError.
🚀 Application
expert3:00remaining
Find roots of polynomial from coefficients
Given polynomial coefficients
[1, -6, 11, -6], which option correctly finds all roots using np.roots?NumPy
import numpy as np coeffs = [1, -6, 11, -6] roots = np.roots(coeffs) print(sorted(roots))
Attempts:
2 left
💡 Hint
np.roots returns roots in any order; sorting helps compare.
✗ Incorrect
Polynomial is (x-1)(x-2)(x-3), roots are 1, 2, 3. Sorted roots are [1.0, 2.0, 3.0].