data_compression.move_to_front

Move-to-front transform.

The move-to-front transform encodes each symbol as its current index in an ordered alphabet, then moves that symbol to the front of the alphabet. It is commonly used after the Burrows-Wheeler transform in lossless compression pipelines.

Reference: https://en.wikipedia.org/wiki/Move-to-front_transform

Functions

_validated_alphabet(→ list[str])

Return a mutable alphabet list after validating uniqueness.

move_to_front_decode(→ str)

Decode a move-to-front encoded list of indexes.

move_to_front_encode(→ list[int])

Encode text using the move-to-front transform.

Module Contents

data_compression.move_to_front._validated_alphabet(alphabet: str) list[str]

Return a mutable alphabet list after validating uniqueness.

>>> _validated_alphabet("abc")
['a', 'b', 'c']
>>> _validated_alphabet("aba")
Traceback (most recent call last):
    ...
ValueError: alphabet must contain unique characters
data_compression.move_to_front.move_to_front_decode(encoded_text: list[int], alphabet: str) str

Decode a move-to-front encoded list of indexes.

>>> move_to_front_decode([1, 1, 13, 1, 1, 1], "abcdefghijklmnopqrstuvwxyz")
'banana'
>>> move_to_front_decode([1, 1, 2, 1, 1, 1], "abn")
'banana'
>>> move_to_front_decode([], "abc")
''
>>> move_to_front_decode([3], "abc")
Traceback (most recent call last):
    ...
ValueError: index 3 is not valid for alphabet size 3
>>> move_to_front_decode([-1], "abc")
Traceback (most recent call last):
    ...
ValueError: index -1 is not valid for alphabet size 3
data_compression.move_to_front.move_to_front_encode(text: str, alphabet: str) list[int]

Encode text using the move-to-front transform.

>>> move_to_front_encode("banana", "abcdefghijklmnopqrstuvwxyz")
[1, 1, 13, 1, 1, 1]
>>> move_to_front_encode("banana", "abn")
[1, 1, 2, 1, 1, 1]
>>> move_to_front_encode("", "abc")
[]
>>> move_to_front_encode("bad", "abc")
Traceback (most recent call last):
    ...
ValueError: character 'd' is not in the alphabet