TheAlgorithms/Python · error · ValueError
max_set_length must be non-negative
Error message
max_set_length must be non-negative
What it means
Raised by bell_numbers() in maths/special_numbers/bell_numbers.py when max_set_length is negative. The function pre-allocates bell = [0] * (max_set_length + 1) and builds the Bell triangle recurrence; a negative length would break indexing and is meaningless (bell_numbers(0) legitimately returns [1]). Only negativity is checked — non-int types are not validated here and may fail separately.
Source
Thrown at maths/special_numbers/bell_numbers.py:36
Bell numbers are calculated.
Returns:
list: A list of Bell numbers for sets of lengths from 0 to max_set_length.
Examples:
>>> bell_numbers(-2)
Traceback (most recent call last):
...
ValueError: max_set_length must be non-negative
>>> bell_numbers(0)
[1]
>>> bell_numbers(1)
[1, 1]
>>> bell_numbers(5)
[1, 1, 2, 5, 15, 52]
"""
if max_set_length < 0:
raise ValueError("max_set_length must be non-negative")
bell = [0] * (max_set_length + 1)
bell[0] = 1
for i in range(1, max_set_length + 1):
for j in range(i):
bell[i] += _binomial_coefficient(i - 1, j) * bell[j]
return bell
def _binomial_coefficient(total_elements: int, elements_to_choose: int) -> int:
"""
Calculate the binomial coefficient C(total_elements, elements_to_choose)
Args:
total_elements (int): The total number of elements.
elements_to_choose (int): The number of elements to choose.View on GitHub (pinned to f5988cc097)
Solutions
- Clamp: bell_numbers(max(0, max_set_length))
- Validate the computation that produces the length: assert k <= n before subtracting
- Return an empty/short list yourself for negative requests instead of calling the function
Example fix
# before bells = bell_numbers(n - k) # negative when k > n # after bells = bell_numbers(n - k) if n >= k else []
Defensive patterns
Strategy: validation
Validate before calling
bells = bell_numbers(max_set_length) if max_set_length >= 0 else []
Type guard
def is_non_negative_int(value: object) -> bool:
return isinstance(value, int) and not isinstance(value, bool) and value >= 0 Prevention
- Clamp computed lengths: max(0, n - k)
- Assert k <= n before subtracting in combinatorics helpers
- bell_numbers(0) is valid and returns [1]
When it happens
Trigger: Calling bell_numbers(-2) or bell_numbers(-1). A computed max_set_length from subtraction (e.g. n - k) that goes negative is the usual source. bell_numbers(0) and bell_numbers(1) are valid and return [1] and [1, 1].
Common situations: Combinatorics helper code where the requested set size exceeds available elements (k > n making n - k negative); CLI args with bad defaults; loop bounds off by one.
Related errors
- Input list must be a non empty list
- Input list must be a non empty list
- Length must be a positive integer.
- {num}: Invalid input, please enter a positive integer.
- Number {n} must instead be a positive integer
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/4b1a86f6fa32a7e0.
Report an issue: GitHub.