TheAlgorithms/Python · error · ValueError

The board should be a non empty matrix of single chars strin

Error message

The board should be a non empty matrix of single chars strings.

What it means

Raised by centripetal() in physics/centripetal_force.py when radius <= 0. Radius is the divisor in (mass * v**2)/radius, so zero would divide by zero and negative radius is unphysical for circular motion. Unlike the mass check, this one rejects zero too.

Source

Thrown at backtracking/word_search.py:132

    ValueError: The board should be a non empty matrix of single chars strings.
    >>> word_exists([], "AB")
    Traceback (most recent call last):
        ...
    ValueError: The board should be a non empty matrix of single chars strings.
    >>> word_exists([["A"], [21]], "AB")
    Traceback (most recent call last):
        ...
    ValueError: The board should be a non empty matrix of single chars strings.
    """

    # Validate board
    board_error_message = (
        "The board should be a non empty matrix of single chars strings."
    )

    len_board = len(board)
    if not isinstance(board, list) or len(board) == 0:
        raise ValueError(board_error_message)

    for row in board:
        if not isinstance(row, list) or len(row) == 0:
            raise ValueError(board_error_message)

        for item in row:
            if not isinstance(item, str) or len(item) != 1:
                raise ValueError(board_error_message)

    # Validate word
    if not isinstance(word, str) or len(word) == 0:
        raise ValueError(
            "The word parameter should be a string of length greater than 0."
        )

    len_board_column = len(board[0])
    for i in range(len_board):
        for j in range(len_board_column):

View on GitHub (pinned to f5988cc097)

Solutions

  1. Ensure radius is a positive number before calling; use a realistic default (e.g. 1.0) instead of 0.
  2. If radius is computed geometrically, guard the degenerate case (identical points) upstream.
  3. Catch ValueError and reject the input record.

Example fix

# before
force = centripetal(m, v, r)  # r may be 0 for coincident points

# after
if r <= 0:
    raise ValueError("degenerate circle: radius must be > 0")
force = centripetal(m, v, r)
Defensive patterns

Strategy: validation

Validate before calling

if radius <= 0:
    raise ValueError(f"radius must be > 0, got {radius}")
f = centripetal(mass, velocity, radius)

Type guard

def is_positive_radius(r: object) -> bool:
    return isinstance(r, (int, float)) and not isinstance(r, bool) and r > 0

Try / catch

try:
    f = centripetal(m, v, r)
except ValueError as e:
    if "radius" in str(e):
        r = r or 1.0  # replace uninitialized default
        f = centripetal(m, v, r)
    else:
        raise

Prevention

When it happens

Trigger: centripetal(10, 15, 0) or centripetal(10, 15, -5); passing a radius of 0 because of an uninitialized variable; computing radius as a difference that collapsed to 0.

Common situations: Default-initialized radius = 0.0 never overwritten; geometry code producing a degenerate circle (radius 0) for coincident points; unit tests passing 0 as a placeholder.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/2d9489f2ff228584. Report an issue: GitHub.