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)
| 67 | |
| 68 | |
| 69 | def 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 | |
| 90 | if __name__ == "__main__": |
no test coverage detected