TheAlgorithms/Python · error · ValueError

The parameter idx_original_string must be lower than len(bwt

Error message

The parameter idx_original_string must be lower than len(bwt_string).

What it means

Raised by reverse_bwt() in data_compression/burrows_wheeler.py when idx_original_string is >= len(bwt_string). The index selects one row from the ordered rotations list, whose length equals the BWT string length, so any index at or beyond that length is invalid.

Source

Thrown at data_compression/burrows_wheeler.py:153

    '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(
        f"Burrows Wheeler transform for string '{s}' results "
        f"in '{result['bwt_string']}'"

View on GitHub (pinned to f5988cc097)

Solutions

  1. Always take bwt_string and idx_original_string from the same bwt_transform() result dict, never mix sources.
  2. Validate before calling: 0 <= idx < len(bwt_string).
  3. If the string was truncated in transit, fix the transport/serialization so the pair stays consistent.

Example fix

# before
plain = reverse_bwt(bwt_str, idx)  # idx == len(bwt_str)

# after
assert 0 <= idx < len(bwt_str), 'bwt_string/idx mismatch'
plain = reverse_bwt(bwt_str, idx)
Defensive patterns

Strategy: validation

Validate before calling

if not 0 <= int(idx_original_string) < len(bwt_string):
    raise ValueError('bwt_string/idx pair is inconsistent')
plain = reverse_bwt(bwt_string, idx_original_string)

Prevention

When it happens

Trigger: Calling reverse_bwt('mnpbnnaaaaaa', 12) (length 12, valid max index 11), or pairing a BWT string and index from different inputs after a data mismatch — e.g. index from one record with the string of another.

Common situations: Data pairs (bwt_string, idx) getting out of sync in a pipeline (partial overwrite, wrong join key), truncated BWT strings making a previously valid index too large, or hand-built test data with an off-by-one index.

Related errors


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