TheAlgorithms/Python · error · ValueError
Invalid source or destination coordinates
Error message
Invalid source or destination coordinates
What it means
Raised by center_of_mass() in physics/center_of_mass.py when any particle in the list has mass <= 0 (zero or negative). A zero-mass particle contributes nothing but still passes the emptiness check, and negative masses make the weighted average meaningless; total_mass could even become 0 and cause a ZeroDivisionError, so the library rejects them up front.
Source
Thrown at backtracking/rat_in_maze.py:127
>>> solve_maze(maze,2,0,len(maze)-1,len(maze)-1)
Traceback (most recent call last):
...
ValueError: Invalid source or destination coordinates
>>> maze = [[1, 0, 0],
... [0, 1, 0],
... [1, 0, 0]]
>>> solve_maze(maze,0,1,len(maze),len(maze)-1)
Traceback (most recent call last):
...
ValueError: Invalid source or destination coordinates
"""
size = len(maze)
# Check if source and destination coordinates are Invalid.
if not (0 <= source_row <= size - 1 and 0 <= source_column <= size - 1) or (
not (0 <= destination_row <= size - 1 and 0 <= destination_column <= size - 1)
):
raise ValueError("Invalid source or destination coordinates")
# We need to create solution object to save path.
solutions = [[1 for _ in range(size)] for _ in range(size)]
solved = run_maze(
maze, source_row, source_column, destination_row, destination_column, solutions
)
if solved:
return solutions
else:
raise ValueError("No solution exists!")
def run_maze(
maze: list[list[int]],
i: int,
j: int,
destination_row: int,
destination_column: int,
solutions: list[list[int]],View on GitHub (pinned to f5988cc097)
Solutions
- Verify Particle field order is (x, y, z, mass) at every construction site.
- Filter or repair particles before the call: [p for p in particles if p.mass > 0].
- Treat mass<=0 rows in source data as corrupt and log/skip them.
Example fix
# before com = center_of_mass(all_particles) # some have mass 0 # after valid = [p for p in all_particles if p.mass > 0] com = center_of_mass(valid) if valid else None
Defensive patterns
Strategy: validation
Validate before calling
valid = [p for p in particles if p.mass > 0]
if not valid:
raise ValueError("no particles with positive mass")
com = center_of_mass(valid) Type guard
from collections import namedtuple
Particle = namedtuple('Particle', 'x y z mass')
def has_valid_masses(ps: list) -> bool:
return all(p.mass > 0 for p in ps) Try / catch
try:
com = center_of_mass(particles)
except ValueError as e:
if "Mass" in str(e):
bad = [i for i, p in enumerate(particles) if p.mass <= 0]
raise ValueError(f"non-positive mass at indices {bad}") from e
raise Prevention
- Particle field order is (x, y, z, mass) — enforce with keywords: Particle(x=.., y=.., z=.., mass=..).
- Reject mass<=0 rows at data ingest.
- Remember 0 mass is also rejected here (unlike centripetal).
When it happens
Trigger: center_of_mass([Particle(0, 0, 0, 0)]); any list containing a Particle whose mass field is 0 or negative, e.g. Particle(9, 10, 11, 0) or Particle(1, 2, 3, -5).
Common situations: Field-order mistakes when constructing Particle(x, y, z, mass) — putting mass in the wrong slot; imported data with missing masses defaulted to 0; placeholder particles added with mass 0.
Related errors
- Expected a_coeffs to have {self.order + 1} elements for {sel
- Expected b_coeffs to have {self.order + 1} elements for {sel
- k must not be negative
- n must not be negative
- All elements in candidates must be non-negative
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/07275db2de396aaa.
Report an issue: GitHub.