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)
| 21 | |
| 22 | |
| 23 | def 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 | |
| 38 | def build_tree(letters: list[Letter]) -> Letter | TreeNode: |