TheAlgorithms/Python · error · TypeError

The parameter idx_original_string type must be int or passiv

Error message

The parameter idx_original_string type must be int or passive of cast to int.

What it means

Raised by reverse_bwt() in data_compression/burrows_wheeler.py when idx_original_string cannot be cast to int via int(). Note the try block catches ValueError only, so floats like 11.0 and 11.4 are accepted (truncated), while non-numeric strings ('11') and None raise this TypeError. Also note complex numbers with an imaginary part raise TypeError inside int() and are not caught by this handler, escaping as a different message.

Source

Thrown at data_compression/burrows_wheeler.py:146

    ValueError: The parameter idx_original_string must not be lower than 0.
    >>> reverse_bwt("mnpbnnaaaaaa", 12) # doctest: +NORMALIZE_WHITESPACE
    Traceback (most recent call last):
        ...
    ValueError: The parameter idx_original_string must be lower than
    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]

View on GitHub (pinned to f5988cc097)

Solutions

  1. Convert to int at the call site: reverse_bwt(bwt_str, int(idx_str)).
  2. Validate the payload schema so idx is stored/loaded as an integer, not a string.
  3. Be aware floats are silently truncated (11.4 becomes 11) — pass real ints to avoid surprising behavior even though it does not raise.

Example fix

# before
reverse_bwt('mnpbnnaaaaaa', '11')  # TypeError

# after
reverse_bwt('mnpbnnaaaaaa', int('11'))  # 'panamabanana'
Defensive patterns

Strategy: type-guard

Validate before calling

idx = int(idx_original_string)  # raises early with your own context
plain = reverse_bwt(bwt_string, idx)

Type guard

def is_int_coercible(value: object) -> bool:
    try:
        int(value)
        return True
    except (TypeError, ValueError):
        return False

Try / catch

try:
    plain = reverse_bwt(bwt_string, idx)
except TypeError:
    raise ValueError(f'idx {idx!r} is not numeric') from None

Prevention

When it happens

Trigger: Calling reverse_bwt('mnpbnnaaaaaa', '11'), reverse_bwt('mnpbnnaaaaaa', None), or reverse_bwt('mnpbnnaaaaaa', [11]). Passing 11.0 or 11.4 does NOT trigger it — those cast fine per the doctests.

Common situations: Loading the index from JSON/config/CLI where it arrives as a string like '11' and is not converted to int; a None default slipping through from an optional field; type drift after refactoring.

Related errors


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