{"record":{"id":"85af74ece54cac19","repo":"TheAlgorithms/Python","slug":"all-numbers-must-be-positive","errorCode":null,"errorMessage":"All numbers must be positive","messagePattern":"All numbers must be positive","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sorts/msd_radix_sort.py","lineNumber":34,"sourceCode":"    :return: Returns the sorted list\n    >>> msd_radix_sort([40, 12, 1, 100, 4])\n    [1, 4, 12, 40, 100]\n    >>> msd_radix_sort([])\n    []\n    >>> msd_radix_sort([123, 345, 123, 80])\n    [80, 123, 123, 345]\n    >>> msd_radix_sort([1209, 834598, 1, 540402, 45])\n    [1, 45, 1209, 540402, 834598]\n    >>> msd_radix_sort([-1, 34, 45])\n    Traceback (most recent call last):\n        ...\n    ValueError: All numbers must be positive\n    \"\"\"\n    if not list_of_ints:\n        return []\n\n    if min(list_of_ints) < 0:\n        raise ValueError(\"All numbers must be positive\")\n\n    most_bits = max(len(bin(x)[2:]) for x in list_of_ints)\n    return _msd_radix_sort(list_of_ints, most_bits)\n\n\ndef _msd_radix_sort(list_of_ints: list[int], bit_position: int) -> list[int]:\n    \"\"\"\n    Sort the given list based on the bit at bit_position. Numbers with a\n    0 at that position will be at the start of the list, numbers with a\n    1 at the end.\n    :param list_of_ints: A list of integers\n    :param bit_position: the position of the bit that gets compared\n    :return: Returns a partially sorted list\n    >>> _msd_radix_sort([45, 2, 32], 1)\n    [2, 32, 45]\n    >>> _msd_radix_sort([10, 4, 12], 2)\n    [4, 12, 10]\n    \"\"\"","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/sorts/msd_radix_sort.py#L16-L52","documentation":"Raised by msd_radix_sort in sorts/msd_radix_sort.py when the input list contains a negative number. The MSD radix sort recursively partitions numbers by individual bits using bin(x)[2:], which has no sign bit representation for negatives, so the function guards with min(list_of_ints) < 0 and rejects the whole list with ValueError. An empty list returns [] before the check, and 0 is fine.","triggerScenarios":"msd_radix_sort([-1, 34, 45]); msd_radix_sort([0, -42, 7]); any single negative value anywhere in the list, even if the rest are valid.","commonSituations":"Feeding sensor/financial data with negatives; reusing the function on user input that allows minus signs; assuming radix sort handles all ints like Python's sorted() does.","solutions":["Use a sort that supports negatives (sorted(), radix sort with sign handling) if negatives are legitimate data.","Offset-encode: shift all values by -min(list) so they become non-negative, sort, then shift back.","Filter or reject negatives before calling: if min(data) < 0: raise ValueError(...)."],"exampleFix":"# before\nmsd_radix_sort([-1, 34, 23, 4, -42])  # ValueError\n\n# after\noffset = min(data)\nshifted = msd_radix_sort([x - offset for x in data])\nsorted_data = [x + offset for x in shifted]","handlingStrategy":"validation","validationCode":"if any(x < 0 for x in data):\n    offset = min(data)\n    data = [x - offset for x in data]\nresult = msd_radix_sort(data)\n# then add offset back to result if original values are needed","typeGuard":"def is_non_negative_ints(lst) -> bool:\n    return all(isinstance(x, int) and x >= 0 for x in lst)","tryCatchPattern":"try:\n    out = msd_radix_sort(data)\nexcept ValueError:\n    m = min(data)\n    out = [x + m for x in msd_radix_sort([x - m for x in data])]","preventionTips":["Radix sorts in this repo assume non-negative ints; check min(data) before calling.","Offset-encode (shift by -min) when negatives are legitimate, and remember to shift back.","Do not assume the function behaves like sorted(); it has a narrower contract."],"tags":["sorting","radix-sort","precondition","non-negative","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}