LoginSignup
0
0

More than 1 year has passed since last update.

Pythonで連長圧縮(Run Length Compression)

Posted at

Pythonで二値連長圧縮

def run_length_compression(array: str) -> list[int]:
    """Return the run length compression of the given binary array.

    Args:
        array (str): The binary array to compress.

    Returns:
        list[int]: The run length compression of the given binary array.
    
    Examples:
        >>> run_length_compression("ABBABBABBB")
        [1, 2, 1, 1, 2, 3]
    """
    compressed = []
    head = array[0]
    count = 0
    for x in array:
        if x == head:
            count += 1
        else:
            compressed.append(count)
            head = x
            count = 1
    compressed.append(count)
    
    return compressed
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0