MCPcopy
hub / github.com/TheAlgorithms/Python / add_key_to_lexicon

Function add_key_to_lexicon

data_compression/lempel_ziv.py:28–41  ·  view source on GitHub ↗

Adds new strings (curr_string + "0", curr_string + "1") to the lexicon

(
    lexicon: dict[str, str], curr_string: str, index: int, last_match_id: str
)

Source from the content-addressed store, hash-verified

26
27
28def add_key_to_lexicon(
29 lexicon: dict[str, str], curr_string: str, index: int, last_match_id: str
30) -> None:
31 """
32 Adds new strings (curr_string + "0", curr_string + "1") to the lexicon
33 """
34 lexicon.pop(curr_string)
35 lexicon[curr_string + "0"] = last_match_id
36
37 if math.log2(index).is_integer():
38 for curr_key, value in lexicon.items():
39 lexicon[curr_key] = f"0{value}"
40
41 lexicon[curr_string + "1"] = bin(index)[2:]
42
43
44def compress_data(data_bits: str) -> str:

Callers 1

compress_dataFunction · 0.85

Calls 1

popMethod · 0.45

Tested by

no test coverage detected