TheAlgorithms/Python · error · ValueError

The parameter idx_original_string must not be lower than 0.

Error message

The parameter idx_original_string must not be lower than 0.

What it means

Raised by reverse_bwt() in data_compression/burrows_wheeler.py when idx_original_string (after int() coercion) is negative. A negative index cannot refer to any position in the ordered rotations list, so it is rejected before the upper-bound check.

Source

Thrown at data_compression/burrows_wheeler.py:151

    len(bwt_string).
    >>> reverse_bwt("mnpbnnaaaaaa", 11.0)
    'panamabanana'
    >>> reverse_bwt("mnpbnnaaaaaa", 11.4)
    'panamabanana'
    """
    if not isinstance(bwt_string, str):
        raise TypeError("The parameter bwt_string type must be str.")
    if not bwt_string:
        raise ValueError("The parameter bwt_string must not be empty.")
    try:
        idx_original_string = int(idx_original_string)
    except ValueError:
        raise TypeError(
            "The parameter idx_original_string type must be int or passive"
            " of cast to int."
        )
    if idx_original_string < 0:
        raise ValueError("The parameter idx_original_string must not be lower than 0.")
    if idx_original_string >= len(bwt_string):
        raise ValueError(
            "The parameter idx_original_string must be lower than len(bwt_string)."
        )

    ordered_rotations = [""] * len(bwt_string)
    for _ in range(len(bwt_string)):
        for i in range(len(bwt_string)):
            ordered_rotations[i] = bwt_string[i] + ordered_rotations[i]
        ordered_rotations.sort()
    return ordered_rotations[idx_original_string]


if __name__ == "__main__":
    entry_msg = "Provide a string that I will generate its BWT transform: "
    s = input(entry_msg).strip()
    result = bwt_transform(s)
    print(

View on GitHub (pinned to f5988cc097)

Solutions

  1. Clamp or validate the index before calling: if idx < 0: handle error; else call.
  2. Trace where the negative index is computed — usually an off-by-sign arithmetic bug upstream.
  3. If the value comes from untrusted data, validate the integer range at the parse boundary.

Example fix

# before
plain = reverse_bwt(bwt_str, idx)  # idx == -1

# after
if idx < 0:
    raise ValueError(f'bad index {idx}')
plain = reverse_bwt(bwt_str, idx)
Defensive patterns

Strategy: validation

Validate before calling

if int(idx_original_string) < 0:
    raise ValueError(f'negative index {idx_original_string}')
plain = reverse_bwt(bwt_string, idx_original_string)

Prevention

When it happens

Trigger: Calling reverse_bwt('mnpbnnaaaaaa', -1), or passing -0.5 (int(-0.5) == 0, so this does NOT raise), or a negative value from arithmetic such as idx = found - offset that underflows.

Common situations: Index arithmetic bugs (subtracting instead of adding), reading a signed field from a binary format where a corrupted sign bit yields a negative number, or unit tests probing boundary conditions.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/c2e903c6d07b176d. Report an issue: GitHub.