MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / decompress

Method decompress

data_compression/lz77.py:111–142  ·  view source on GitHub ↗

Convert the list of tokens into an output string. Args: tokens: list containing triplets (offset, length, char) Returns: output: decompressed text Tests: >>> lz77_compressor = LZ77Compressor() >>> lz77_compressor.dec

(self, tokens: list[Token])

Source from the content-addressed store, hash-verified

109 return output
110
111 def decompress(self, tokens: list[Token]) -> str:
112 """
113 Convert the list of tokens into an output string.
114
115 Args:
116 tokens: list containing triplets (offset, length, char)
117
118 Returns:
119 output: decompressed text
120
121 Tests:
122 >>> lz77_compressor = LZ77Compressor()
123 >>> lz77_compressor.decompress([Token(0, 0, 'c'), Token(0, 0, 'a'),
124 ... Token(0, 0, 'b'), Token(0, 0, 'r'), Token(3, 1, 'c'),
125 ... Token(2, 1, 'd'), Token(7, 4, 'r'), Token(3, 5, 'd')])
126 'cabracadabrarrarrad'
127 >>> lz77_compressor.decompress([Token(0, 0, 'a'), Token(0, 0, 'b'),
128 ... Token(2, 2, 'c'), Token(4, 3, 'a'), Token(2, 2, 'a')])
129 'ababcbababaa'
130 >>> lz77_compressor.decompress([Token(0, 0, 'a'), Token(1, 1, 'c'),
131 ... Token(3, 4, 'b'), Token(3, 3, 'a'), Token(1, 2, 'c')])
132 'aacaacabcabaaac'
133 """
134
135 output = ""
136
137 for token in tokens:
138 for _ in range(token.length):
139 output += output[-token.offset]
140 output += token.indicator
141
142 return output
143
144 def _find_encoding_token(self, text: str, search_buffer: str) -> Token:
145 """Finds the encoding token for the first character in the text.

Callers 1

lz77.pyFile · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected