{"record":{"id":"a3f225b22e2b54d0","repo":"TheAlgorithms/Python","slug":"sequence-must-be-list-of-non-negative-integers","errorCode":null,"errorMessage":"Sequence must be list of non-negative integers","messagePattern":"Sequence must be list of non-negative integers","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sorts/bead_sort.py","lineNumber":32,"sourceCode":"\n    >>> bead_sort([5, 0, 4, 3])\n    [0, 3, 4, 5]\n\n    >>> bead_sort([8, 2, 1])\n    [1, 2, 8]\n\n    >>> bead_sort([1, .9, 0.0, 0, -1, -.9])\n    Traceback (most recent call last):\n        ...\n    TypeError: Sequence must be list of non-negative integers\n\n    >>> bead_sort(\"Hello world\")\n    Traceback (most recent call last):\n        ...\n    TypeError: Sequence must be list of non-negative integers\n    \"\"\"\n    if any(not isinstance(x, int) or x < 0 for x in sequence):\n        raise TypeError(\"Sequence must be list of non-negative integers\")\n    for _ in range(len(sequence)):\n        for i, (rod_upper, rod_lower) in enumerate(zip(sequence, sequence[1:])):  # noqa: RUF007\n            if rod_upper > rod_lower:\n                sequence[i] -= rod_upper - rod_lower\n                sequence[i + 1] += rod_upper - rod_lower\n    return sequence\n\n\nif __name__ == \"__main__\":\n    assert bead_sort([5, 4, 3, 2, 1]) == [1, 2, 3, 4, 5]\n    assert bead_sort([7, 9, 4, 3, 5]) == [3, 4, 5, 7, 9]\n","sourceCodeStart":14,"sourceCodeEnd":44,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/sorts/bead_sort.py#L14-L44","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\nbead_sort([5.0, 3, 1])  # 5.0 is float -> TypeError\n\n# after\nbead_sort([int(x) for x in [5.0, 3, 1]])","handlingStrategy":"type-guard","validationCode":"def to_bead_input(seq):\n    out = []\n    for x in seq:\n        if not isinstance(x, int) or isinstance(x, bool) or x < 0:\n            raise TypeError(f'bead_sort needs non-negative ints, got {x!r}')\n        out.append(x)\n    return out\n\nresult = bead_sort(to_bead_input(data))","typeGuard":"def is_non_negative_int_list(seq) -> bool:\n    return all(\n        isinstance(x, int) and not isinstance(x, bool) and x >= 0 for x in seq\n    )","tryCatchPattern":"try:\n    result = bead_sort(data)\nexcept TypeError:\n    result = bead_sort([int(x) for x in data])  # only if conversion is lossless","preventionTips":["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."],"tags":["sorting","bead-sort","type-error","precondition","non-negative-ints"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}