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

Function huffman

data_compression/huffman.py:69–87  ·  view source on GitHub ↗

Parse the file, build the tree, then run through the file again, using the letters dictionary to find and print out the bitstring for each letter.

(file_path: str)

Source from the content-addressed store, hash-verified

67
68
69def huffman(file_path: str) -> None:
70 """
71 Parse the file, build the tree, then run through the file
72 again, using the letters dictionary to find and print out the
73 bitstring for each letter.
74 """
75 letters_list = parse_file(file_path)
76 root = build_tree(letters_list)
77 letters = {
78 k: v for letter in traverse_tree(root, "") for k, v in letter.bitstring.items()
79 }
80 print(f"Huffman Coding of {file_path}: ")
81 with open(file_path) as f:
82 while True:
83 c = f.read(1)
84 if not c:
85 break
86 print(letters[c], end=" ")
87 print()
88
89
90if __name__ == "__main__":

Callers 1

huffman.pyFile · 0.85

Calls 3

parse_fileFunction · 0.85
traverse_treeFunction · 0.85
build_treeFunction · 0.70

Tested by

no test coverage detected