TheAlgorithms/Python · error · ValueError
All numbers must be positive
Error message
All numbers must be positive
What it means
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.
Source
Thrown at sorts/msd_radix_sort.py:34
:return: Returns the sorted list
>>> msd_radix_sort([40, 12, 1, 100, 4])
[1, 4, 12, 40, 100]
>>> msd_radix_sort([])
[]
>>> msd_radix_sort([123, 345, 123, 80])
[80, 123, 123, 345]
>>> msd_radix_sort([1209, 834598, 1, 540402, 45])
[1, 45, 1209, 540402, 834598]
>>> msd_radix_sort([-1, 34, 45])
Traceback (most recent call last):
...
ValueError: All numbers must be positive
"""
if not list_of_ints:
return []
if min(list_of_ints) < 0:
raise ValueError("All numbers must be positive")
most_bits = max(len(bin(x)[2:]) for x in list_of_ints)
return _msd_radix_sort(list_of_ints, most_bits)
def _msd_radix_sort(list_of_ints: list[int], bit_position: int) -> list[int]:
"""
Sort the given list based on the bit at bit_position. Numbers with a
0 at that position will be at the start of the list, numbers with a
1 at the end.
:param list_of_ints: A list of integers
:param bit_position: the position of the bit that gets compared
:return: Returns a partially sorted list
>>> _msd_radix_sort([45, 2, 32], 1)
[2, 32, 45]
>>> _msd_radix_sort([10, 4, 12], 2)
[4, 12, 10]
"""View on GitHub (pinned to f5988cc097)
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(...).
Example fix
# before msd_radix_sort([-1, 34, 23, 4, -42]) # ValueError # after offset = min(data) shifted = msd_radix_sort([x - offset for x in data]) sorted_data = [x + offset for x in shifted]
Defensive patterns
Strategy: validation
Validate before calling
if any(x < 0 for x in data):
offset = min(data)
data = [x - offset for x in data]
result = msd_radix_sort(data)
# then add offset back to result if original values are needed Type guard
def is_non_negative_ints(lst) -> bool:
return all(isinstance(x, int) and x >= 0 for x in lst) Try / catch
try:
out = msd_radix_sort(data)
except ValueError:
m = min(data)
out = [x + m for x in msd_radix_sort([x - m for x in data])] Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- number of qubits too large to simulate(>10).
- k integer must be greater or equal to zero.
- Sequence must be list of non-negative integers
- The elements inside the sequence must contains only {colors}
- Expected a_coeffs to have {self.order + 1} elements for {sel
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/85af74ece54cac19.
Report an issue: GitHub.