MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / parse_file

Function parse_file

data_compression/huffman.py:23–35  ·  view source on GitHub ↗

Read the file and build a dict of all letters and their frequencies, then convert the dict into a list of Letters.

(file_path: str)

Source from the content-addressed store, hash-verified

21
22
23def parse_file(file_path: str) -> list[Letter]:
24 """
25 Read the file and build a dict of all letters and their
26 frequencies, then convert the dict into a list of Letters.
27 """
28 chars: dict[str, int] = {}
29 with open(file_path) as f:
30 while True:
31 c = f.read(1)
32 if not c:
33 break
34 chars[c] = chars[c] + 1 if c in chars else 1
35 return sorted((Letter(c, f) for c, f in chars.items()), key=lambda x: x.freq)
36
37
38def build_tree(letters: list[Letter]) -> Letter | TreeNode:

Callers 1

huffmanFunction · 0.85

Calls 1

LetterClass · 0.85

Tested by

no test coverage detected