{"record":{"id":"11d659bf201d2468","repo":"TheAlgorithms/Python","slug":"n-must-not-be-negative","errorCode":null,"errorMessage":"n must not be negative","messagePattern":"n must not be negative","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"backtracking/all_combinations.py","lineNumber":57,"sourceCode":"    >>> generate_all_combinations(n=5, k=4)\n    [[1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 4, 5], [1, 3, 4, 5], [2, 3, 4, 5]]\n    >>> generate_all_combinations(n=3, k=3)\n    [[1, 2, 3]]\n    >>> generate_all_combinations(n=3, k=1)\n    [[1], [2], [3]]\n    >>> generate_all_combinations(n=1, k=0)\n    [[]]\n    >>> generate_all_combinations(n=1, k=1)\n    [[1]]\n    >>> from itertools import combinations\n    >>> all(generate_all_combinations(n, k) == combination_lists(n, k)\n    ...     for n in range(1, 6) for k in range(1, 6))\n    True\n    \"\"\"\n    if k < 0:\n        raise ValueError(\"k must not be negative\")\n    if n < 0:\n        raise ValueError(\"n must not be negative\")\n\n    result: list[list[int]] = []\n    create_all_state(1, n, k, [], result)\n    return result\n\n\ndef create_all_state(\n    increment: int,\n    total_number: int,\n    level: int,\n    current_list: list[int],\n    total_list: list[list[int]],\n) -> None:\n    \"\"\"\n    Helper function to recursively build all combinations.\n\n    >>> create_all_state(1, 4, 2, [], result := [])\n    >>> result","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/backtracking/all_combinations.py#L39-L75","documentation":"Raised by capture_area() in physics/basic_orbital_capture.py when the capture radius passed in is negative. The function computes sigma = pi * r^2, the effective cross-sectional capture area; a negative radius has no physical meaning. The guard sits between the docstring and the pi*r**2 computation.","triggerScenarios":"Calling capture_area(-1); passing the unvalidated result of another computation that produced a negative radius; chaining capture_radii output through a subtraction that flipped sign.","commonSituations":"Reusing a radius variable that was negated elsewhere, or passing an error code / sentinel negative number straight into the function without checking.","solutions":["Validate that the radius is >= 0 before calling capture_area().","If the radius comes from capture_radii(), inspect why it went negative — that function itself already guards mass/radius, so the sign flip happened in your code.","Use abs() only if the negative sign is provably a sign-convention artifact, not bad data."],"exampleFix":"# before\narea = capture_area(some_radius)\n\n# after\nif some_radius < 0:\n    raise ValueError(f\"radius must be >= 0, got {some_radius}\")\narea = capture_area(some_radius)","handlingStrategy":"validation","validationCode":"if capture_radius < 0:\n    raise ValueError(f\"capture_radius must be >= 0, got {capture_radius}\")\nsigma = capture_area(capture_radius)","typeGuard":"def is_non_negative(x: object) -> bool:\n    return isinstance(x, (int, float)) and not isinstance(x, bool) and x >= 0","tryCatchPattern":"try:\n    sigma = capture_area(r)\nexcept ValueError:\n    sigma = capture_area(abs(r))  # only when sign is a proven artifact","preventionTips":["Chain capture_radii() output directly; it never returns negative radii.","Validate intermediate radius arithmetic for sign flips.","Unit-test the boundary r=0 (returns 0.0, does not raise)."],"tags":["physics","input-validation","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}