TheAlgorithms/Python · error · TypeError
Sequence must be list of non-negative integers
Error message
Sequence must be list of non-negative integers
What it means
Raised by bead_sort in sorts/bead_sort.py when any element of sequence is not an int or is negative. Bead sort physically models the numbers as beads on rods, which only works for non-negative integers, so the guard `any(not isinstance(x, int) or x < 0 for x in sequence)` rejects floats (including 0.0 and .9), strings, and negatives with a TypeError. Quirk: bool passes the check because bool is a subclass of int.
Source
Thrown at sorts/bead_sort.py:32
>>> bead_sort([5, 0, 4, 3])
[0, 3, 4, 5]
>>> bead_sort([8, 2, 1])
[1, 2, 8]
>>> bead_sort([1, .9, 0.0, 0, -1, -.9])
Traceback (most recent call last):
...
TypeError: Sequence must be list of non-negative integers
>>> bead_sort("Hello world")
Traceback (most recent call last):
...
TypeError: Sequence must be list of non-negative integers
"""
if any(not isinstance(x, int) or x < 0 for x in sequence):
raise TypeError("Sequence must be list of non-negative integers")
for _ in range(len(sequence)):
for i, (rod_upper, rod_lower) in enumerate(zip(sequence, sequence[1:])): # noqa: RUF007
if rod_upper > rod_lower:
sequence[i] -= rod_upper - rod_lower
sequence[i + 1] += rod_upper - rod_lower
return sequence
if __name__ == "__main__":
assert bead_sort([5, 4, 3, 2, 1]) == [1, 2, 3, 4, 5]
assert bead_sort([7, 9, 4, 3, 5]) == [3, 4, 5, 7, 9]
View on GitHub (pinned to f5988cc097)
Solutions
- Convert to non-negative ints first: bead_sort([int(x) for x in sequence]).
- Reject or clean floats before calling: if any(not float(x).is_integer() for x in seq): ...
- For data that may contain negatives, shift by the minimum (offset encoding) or use a comparison sort instead.
Example fix
# before bead_sort([5.0, 3, 1]) # 5.0 is float -> TypeError # after bead_sort([int(x) for x in [5.0, 3, 1]])
Defensive patterns
Strategy: type-guard
Validate before calling
def to_bead_input(seq):
out = []
for x in seq:
if not isinstance(x, int) or isinstance(x, bool) or x < 0:
raise TypeError(f'bead_sort needs non-negative ints, got {x!r}')
out.append(x)
return out
result = bead_sort(to_bead_input(data)) Type guard
def is_non_negative_int_list(seq) -> bool:
return all(
isinstance(x, int) and not isinstance(x, bool) and x >= 0 for x in seq
) Try / catch
try:
result = bead_sort(data)
except TypeError:
result = bead_sort([int(x) for x in data]) # only if conversion is lossless Prevention
- Coerce JSON/pandas floats to int before sorting; check float.is_integer() first.
- bool is accepted as int by this guard — exclude it if True/False in data is a bug.
- For lists that may contain negatives, bead_sort is the wrong algorithm; pick a comparison sort.
When it happens
Trigger: bead_sort([1, .9, 0.0, 0, -1, -.9]); bead_sort('Hello world') (iterating a string yields 1-char strs); bead_sort([3.0, 2]) where JSON parsing produced floats.
Common situations: Feeding data straight from json.load where whole numbers became floats; mixing numeric types from pandas/numpy (np.int64 is not a Python int on some platforms and will be rejected); validating user CSV input that contains blanks parsed as NaN.
Related errors
- The elements inside the sequence must contains only {colors}
- All numbers must be positive
- operation can not be conducted on an object of type {type(nu
- Input value must be an 'int' type
- plaintext must be a string
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/a3f225b22e2b54d0.
Report an issue: GitHub.