{"record":{"id":"66c053dfbf12e4df","repo":"TheAlgorithms/Python","slug":"the-program-cannot-work-out-a-fitting-polynomial","errorCode":null,"errorMessage":"The program cannot work out a fitting polynomial.","messagePattern":"The program cannot work out a fitting polynomial\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"linear_algebra/src/polynom_for_points.py","lineNumber":38,"sourceCode":"    >>> points_to_polynomial([[1, 1], [2, 2], [3, 3]])\n    'f(x)=x^2*0.0+x^1*1.0+x^0*0.0'\n    >>> points_to_polynomial([[1, 1], [2, 4], [3, 9]])\n    'f(x)=x^2*1.0+x^1*-0.0+x^0*0.0'\n    >>> points_to_polynomial([[1, 3], [2, 6], [3, 11]])\n    'f(x)=x^2*1.0+x^1*-0.0+x^0*2.0'\n    >>> points_to_polynomial([[1, -3], [2, -6], [3, -11]])\n    'f(x)=x^2*-1.0+x^1*-0.0+x^0*-2.0'\n    >>> points_to_polynomial([[1, 5], [2, 2], [3, 9]])\n    'f(x)=x^2*5.0+x^1*-18.0+x^0*18.0'\n    >>> points_to_polynomial([[1, 1], [1, 2], [1, 3]])\n    'x=1'\n    >>> points_to_polynomial([[1, 1], [2, 2], [2, 2]])\n    Traceback (most recent call last):\n        ...\n    ValueError: The program cannot work out a fitting polynomial.\n    \"\"\"\n    if len(coordinates) == 0 or not all(len(pair) == 2 for pair in coordinates):\n        raise ValueError(\"The program cannot work out a fitting polynomial.\")\n\n    if len({tuple(pair) for pair in coordinates}) != len(coordinates):\n        raise ValueError(\"The program cannot work out a fitting polynomial.\")\n\n    set_x = {x for x, _ in coordinates}\n    if len(set_x) == 1:\n        return f\"x={coordinates[0][0]}\"\n\n    if len(set_x) != len(coordinates):\n        raise ValueError(\"The program cannot work out a fitting polynomial.\")\n\n    x = len(coordinates)\n\n    # put the x and x to the power values in a matrix\n    matrix: list[list[float]] = [\n        [\n            coordinates[count_of_line][0] ** (x - (count_in_line + 1))\n            for count_in_line in range(x)","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/linear_algebra/src/polynom_for_points.py#L20-L56","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"# before\npoints_to_polynomial([[1, 2, 3], [4, 5]])\n\n# after\npoints_to_polynomial([(1, 2), (4, 5)])","handlingStrategy":"validation","validationCode":"def valid_coordinates(coords):\n    return len(coords) > 0 and all(len(p) == 2 for p in coords)\n\nif not valid_coordinates(coordinates):\n    raise ValueError(\"coordinates must be a non-empty list of (x, y) pairs\")\npoly = points_to_polynomial(coordinates)","typeGuard":"def is_valid_point_list(coords: list) -> bool:\n    return bool(coords) and all(\n        isinstance(p, (list, tuple)) and len(p) == 2 for p in coords\n    )","tryCatchPattern":"try:\n    poly = points_to_polynomial(coordinates)\nexcept ValueError as e:\n    logger.error(\"invalid coordinates input: %s\", e)\n    raise","preventionTips":["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."],"tags":["linear-algebra","polynomial","input-validation","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}