{"record":{"id":"4b1a86f6fa32a7e0","repo":"TheAlgorithms/Python","slug":"max-set-length-must-be-non-negative","errorCode":null,"errorMessage":"max_set_length must be non-negative","messagePattern":"max_set_length must be non-negative","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/special_numbers/bell_numbers.py","lineNumber":36,"sourceCode":"        Bell numbers are calculated.\n\n    Returns:\n        list: A list of Bell numbers for sets of lengths from 0 to max_set_length.\n\n    Examples:\n    >>> bell_numbers(-2)\n    Traceback (most recent call last):\n        ...\n    ValueError: max_set_length must be non-negative\n    >>> bell_numbers(0)\n    [1]\n    >>> bell_numbers(1)\n    [1, 1]\n    >>> bell_numbers(5)\n    [1, 1, 2, 5, 15, 52]\n    \"\"\"\n    if max_set_length < 0:\n        raise ValueError(\"max_set_length must be non-negative\")\n\n    bell = [0] * (max_set_length + 1)\n    bell[0] = 1\n\n    for i in range(1, max_set_length + 1):\n        for j in range(i):\n            bell[i] += _binomial_coefficient(i - 1, j) * bell[j]\n\n    return bell\n\n\ndef _binomial_coefficient(total_elements: int, elements_to_choose: int) -> int:\n    \"\"\"\n    Calculate the binomial coefficient C(total_elements, elements_to_choose)\n\n    Args:\n        total_elements (int): The total number of elements.\n        elements_to_choose (int): The number of elements to choose.","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/special_numbers/bell_numbers.py#L18-L54","documentation":"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.","triggerScenarios":"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].","commonSituations":"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.","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"],"exampleFix":"# before\nbells = bell_numbers(n - k)  # negative when k > n\n\n# after\nbells = bell_numbers(n - k) if n >= k else []","handlingStrategy":"validation","validationCode":"bells = bell_numbers(max_set_length) if max_set_length >= 0 else []","typeGuard":"def is_non_negative_int(value: object) -> bool:\n    return isinstance(value, int) and not isinstance(value, bool) and value >= 0","tryCatchPattern":null,"preventionTips":["Clamp computed lengths: max(0, n - k)","Assert k <= n before subtracting in combinatorics helpers","bell_numbers(0) is valid and returns [1]"],"tags":["python","math","combinatorics","validation","value-error"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}