{"record":{"id":"991f7a7089f7419b","repo":"TheAlgorithms/Python","slug":"k-must-not-be-negative","errorCode":null,"errorMessage":"k must not be negative","messagePattern":"k must not be negative","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"backtracking/all_combinations.py","lineNumber":55,"sourceCode":"        ...\n    ValueError: n must not be negative\n    >>> 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","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/backtracking/all_combinations.py#L37-L73","documentation":"Raised by capture_radii() in physics/basic_orbital_capture.py when projectile_velocity exceeds c (the speed of light, ~2.998e8 m/s). Newtonian capture physics is invalid at or above light speed, so the library refuses the input rather than returning a wrong answer. Note the check is strictly greater-than: exactly c is accepted even though that is itself unphysical.","triggerScenarios":"Passing projectile_velocity in km/s instead of m/s (e.g. 30000 km/s = 3e7... but 3e8+ values), or simulating relativistic projectiles (velocity = 3e8 m/s) without realizing this is a Newtonian model.","commonSituations":"Unit mismatch: velocity supplied in km/s while the function expects m/s is the classic case (values above 2.998e5 km/s trip it). Also attempting sci-fi or particle-physics speed regimes with a classical formula.","solutions":["Convert the velocity to m/s before calling (multiply km/s by 1000).","Cap or reject velocities >= c in your own pipeline before invoking the function.","If you genuinely need relativistic capture, use a relativistic model; this library will not accept such inputs."],"exampleFix":"# before\ncapture_radii(6.957e8, 1.99e30, 3e8 + 1)  # velocity above c -> ValueError\n\n# after\nvelocity_ms = velocity_kms * 1000\nassert velocity_ms < 299_792_458\ncapture_radii(6.957e8, 1.99e30, velocity_ms)","handlingStrategy":"validation","validationCode":"C = 299_792_458.0\nif projectile_velocity >= C:\n    raise ValueError(\"velocity at/above c is outside the Newtonian model\")\nr = capture_radii(mass, radius, projectile_velocity)","typeGuard":null,"tryCatchPattern":"try:\n    r = capture_radii(m, R, v)\nexcept ValueError as e:\n    if \"speed of light\" in str(e):\n        v = v / 1000  # km/s -> m/s fixup, then retry once\n        r = capture_radii(m, R, v)\n    else:\n        raise","preventionTips":["Standardize on SI (m/s) for all velocities.","Assert v < 299792458 in simulation setup code.","Remember exactly-c is accepted by the check but still unphysical."],"tags":["physics","units","speed-of-light","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}