TheAlgorithms/Python · error · ValueError
The program cannot work out a fitting polynomial.
Error message
The program cannot work out a fitting polynomial.
What it means
Raised by points_to_polynomial when the coordinates argument is empty or contains elements that are not (x, y) pairs. The function fits a polynomial through points via a Vandermonde-style linear system, so it needs at least one well-formed 2-element pair to build that system. Malformed input makes the system unbuildable, so it fails fast with ValueError.
Source
Thrown at linear_algebra/src/polynom_for_points.py:38
>>> points_to_polynomial([[1, 1], [2, 2], [3, 3]])
'f(x)=x^2*0.0+x^1*1.0+x^0*0.0'
>>> points_to_polynomial([[1, 1], [2, 4], [3, 9]])
'f(x)=x^2*1.0+x^1*-0.0+x^0*0.0'
>>> points_to_polynomial([[1, 3], [2, 6], [3, 11]])
'f(x)=x^2*1.0+x^1*-0.0+x^0*2.0'
>>> points_to_polynomial([[1, -3], [2, -6], [3, -11]])
'f(x)=x^2*-1.0+x^1*-0.0+x^0*-2.0'
>>> points_to_polynomial([[1, 5], [2, 2], [3, 9]])
'f(x)=x^2*5.0+x^1*-18.0+x^0*18.0'
>>> points_to_polynomial([[1, 1], [1, 2], [1, 3]])
'x=1'
>>> points_to_polynomial([[1, 1], [2, 2], [2, 2]])
Traceback (most recent call last):
...
ValueError: The program cannot work out a fitting polynomial.
"""
if len(coordinates) == 0 or not all(len(pair) == 2 for pair in coordinates):
raise ValueError("The program cannot work out a fitting polynomial.")
if len({tuple(pair) for pair in coordinates}) != len(coordinates):
raise ValueError("The program cannot work out a fitting polynomial.")
set_x = {x for x, _ in coordinates}
if len(set_x) == 1:
return f"x={coordinates[0][0]}"
if len(set_x) != len(coordinates):
raise ValueError("The program cannot work out a fitting polynomial.")
x = len(coordinates)
# put the x and x to the power values in a matrix
matrix: list[list[float]] = [
[
coordinates[count_of_line][0] ** (x - (count_in_line + 1))
for count_in_line in range(x)View on GitHub (pinned to f5988cc097)
Solutions
- Ensure every element of the input list is exactly a 2-element pair, e.g. [(x0, y0), (x1, y1), ...].
- Filter or reject malformed rows before calling: coordinates = [p for p in data if len(p) == 2].
- If the input is empty, either skip the call or supply at least one valid point.
Example fix
# before points_to_polynomial([[1, 2, 3], [4, 5]]) # after points_to_polynomial([(1, 2), (4, 5)])
Defensive patterns
Strategy: validation
Validate before calling
def valid_coordinates(coords):
return len(coords) > 0 and all(len(p) == 2 for p in coords)
if not valid_coordinates(coordinates):
raise ValueError("coordinates must be a non-empty list of (x, y) pairs")
poly = points_to_polynomial(coordinates) Type guard
def is_valid_point_list(coords: list) -> bool:
return bool(coords) and all(
isinstance(p, (list, tuple)) and len(p) == 2 for p in coords
) Try / catch
try:
poly = points_to_polynomial(coordinates)
except ValueError as e:
logger.error("invalid coordinates input: %s", e)
raise Prevention
- Normalize incoming data to a list of 2-element tuples before calling.
- Validate row width when reading points from CSV/JSON sources.
- Reject empty inputs at the API boundary instead of relying on the library.
When it happens
Trigger: Calling points_to_polynomial([]) with an empty list, or passing pairs of the wrong arity such as [[1, 2, 3]] or [[1]] or [[1, 2], [3]] (any element whose len() != 2).
Common situations: Passing unvalidated data from a CSV/JSON file where rows have extra columns, passing a flat list of numbers instead of pairs, or passing a list of triples from a 3D-point pipeline.
Related errors
- Expected a_coeffs to have {self.order + 1} elements for {sel
- n must not be negative
- Depth cannot be less than 0
- The parameter costs should be a list of three integers
- number_of_years must be > 0
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/66c053dfbf12e4df.
Report an issue: GitHub.