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