{"record":{"id":"ec89f4306c339773","repo":"TheAlgorithms/Python","slug":"the-parameter-idx-original-string-must-be-lower-th","errorCode":null,"errorMessage":"The parameter idx_original_string must be lower than len(bwt_string).","messagePattern":"The parameter idx_original_string must be lower than len\\(bwt_string\\)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"data_compression/burrows_wheeler.py","lineNumber":153,"sourceCode":"    'panamabanana'\n    >>> reverse_bwt(\"mnpbnnaaaaaa\", 11.4)\n    'panamabanana'\n    \"\"\"\n    if not isinstance(bwt_string, str):\n        raise TypeError(\"The parameter bwt_string type must be str.\")\n    if not bwt_string:\n        raise ValueError(\"The parameter bwt_string must not be empty.\")\n    try:\n        idx_original_string = int(idx_original_string)\n    except ValueError:\n        raise TypeError(\n            \"The parameter idx_original_string type must be int or passive\"\n            \" of cast to int.\"\n        )\n    if idx_original_string < 0:\n        raise ValueError(\"The parameter idx_original_string must not be lower than 0.\")\n    if idx_original_string >= len(bwt_string):\n        raise ValueError(\n            \"The parameter idx_original_string must be lower than len(bwt_string).\"\n        )\n\n    ordered_rotations = [\"\"] * len(bwt_string)\n    for _ in range(len(bwt_string)):\n        for i in range(len(bwt_string)):\n            ordered_rotations[i] = bwt_string[i] + ordered_rotations[i]\n        ordered_rotations.sort()\n    return ordered_rotations[idx_original_string]\n\n\nif __name__ == \"__main__\":\n    entry_msg = \"Provide a string that I will generate its BWT transform: \"\n    s = input(entry_msg).strip()\n    result = bwt_transform(s)\n    print(\n        f\"Burrows Wheeler transform for string '{s}' results \"\n        f\"in '{result['bwt_string']}'\"","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/data_compression/burrows_wheeler.py#L135-L171","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Always take bwt_string and idx_original_string from the same bwt_transform() result dict, never mix sources.","Validate before calling: 0 <= idx < len(bwt_string).","If the string was truncated in transit, fix the transport/serialization so the pair stays consistent."],"exampleFix":"# before\nplain = reverse_bwt(bwt_str, idx)  # idx == len(bwt_str)\n\n# after\nassert 0 <= idx < len(bwt_str), 'bwt_string/idx mismatch'\nplain = reverse_bwt(bwt_str, idx)","handlingStrategy":"validation","validationCode":"if not 0 <= int(idx_original_string) < len(bwt_string):\n    raise ValueError('bwt_string/idx pair is inconsistent')\nplain = reverse_bwt(bwt_string, idx_original_string)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always take bwt_string and idx from the same bwt_transform() result","Never truncate the BWT string without adjusting the stored index"],"tags":["value-validation","burrows-wheeler","index","off-by-one"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}