data_compression.move_to_front ============================== .. py:module:: data_compression.move_to_front .. autoapi-nested-parse:: 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 --------- .. autoapisummary:: data_compression.move_to_front._validated_alphabet data_compression.move_to_front.move_to_front_decode data_compression.move_to_front.move_to_front_encode Module Contents --------------- .. py:function:: _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 .. py:function:: 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 .. py:function:: 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