1
1
from __future__ import annotations
2
2
3
- import sys
4
-
5
3
6
4
class Letter :
7
5
def __init__ (self , letter : str , freq : int ):
@@ -25,11 +23,11 @@ def parse_file(file_path: str) -> list[Letter]:
25
23
Read the file and build a dict of all letters and their
26
24
frequencies, then convert the dict into a list of Letters.
27
25
28
- # >>> file_path = 'file_to_read.txt'
29
- # >>> print(open(file_path, 'r').read()) # showing content of file
30
- # This is the text contained in the file
31
- # >>> parse_file(file_path)
32
- # [T:1, x:1, c:1, o:1, a:1, d:1, f:1, l:1, s:2, h:3, n:3, i:5, t:5, e:5, :7]
26
+ >>> file_path = 'file_to_read.txt'
27
+ >>> print(open(file_path, 'r').read()) # showing content of file
28
+ This is the text contained in the file
29
+ >>> parse_file(file_path)
30
+ [T:1, x:1, c:1, o:1, a:1, d:1, f:1, l:1, s:2, h:3, n:3, i:5, t:5, e:5, :7]
33
31
34
32
"""
35
33
chars : dict [str , int ] = {}
@@ -47,11 +45,11 @@ def build_tree(letters: list[Letter]) -> Letter | TreeNode:
47
45
Run through the list of Letters and build the min heap
48
46
for the Huffman Tree.
49
47
50
- # >>> result_from_parse_file_func = parse_file('file_to_read.txt')
51
- # >>> result_from_parse_file_func
52
- # [T:1, x:1, c:1, o:1, a:1, d:1, f:1, l:1, s:2, h:3, n:3, i:5, t:5, e:5, :7]
53
- # >>> build_tree(result_from_parse_file_func)
54
- # <__main__.TreeNode object at 0x7fb08adff810>
48
+ >>> result_from_parse_file_func = parse_file('file_to_read.txt')
49
+ >>> result_from_parse_file_func
50
+ [T:1, x:1, c:1, o:1, a:1, d:1, f:1, l:1, s:2, h:3, n:3, i:5, t:5, e:5, :7]
51
+ >>> build_tree(result_from_parse_file_func)
52
+ <__main__.TreeNode object at 0x7fb08adff810>
55
53
56
54
"""
57
55
response : list [Letter | TreeNode ] = letters # type: ignore
@@ -70,12 +68,12 @@ def traverse_tree(root: Letter | TreeNode, bitstring: str) -> list[Letter]:
70
68
Recursively traverse the Huffman Tree to set each
71
69
Letter's bitstring dictionary, and return the list of Letters
72
70
73
- # >>> result_from_parse_file_func = parse_file('file_to_read.txt')
74
- # >>> result_from_build_tree_func = build_tree(result_from_parse_file_func)
75
- # >>> result_from_build_tree_func
76
- # <huffman.TreeNode object at 0x10c0cf8c0>
77
- # >>> traverse_tree(result_from_build_tree_func, "")
78
- # [n:3, s:2, T:1, x:1, c:1, o:1, a:1, d:1, i:5, t:5, e:5, f:1, l:1, h:3, :7]
71
+ >>> result_from_parse_file_func = parse_file('file_to_read.txt')
72
+ >>> result_from_build_tree_func = build_tree(result_from_parse_file_func)
73
+ >>> result_from_build_tree_func
74
+ <huffman.TreeNode object at 0x10c0cf8c0>
75
+ >>> traverse_tree(result_from_build_tree_func, "")
76
+ [n:3, s:2, T:1, x:1, c:1, o:1, a:1, d:1, i:5, t:5, e:5, f:1, l:1, h:3, :7]
79
77
80
78
"""
81
79
if isinstance (root , Letter ):
@@ -94,16 +92,16 @@ def huffman(file_path: str) -> None:
94
92
again, using the letters dictionary to find and print out the
95
93
bitstring for each letter.
96
94
97
- # >>> file_path = 'file_to_read.txt'
98
- # >>> print(open(file_path, 'r').read())
99
- # This is the text contained in the file
95
+ >>> file_path = 'file_to_read.txt'
96
+ >>> print(open(file_path, 'r').read())
97
+ This is the text contained in the file
100
98
101
- # >>> huffman(file_path)
102
- # Huffman Coding of file_to_read.txt:
103
- # 00110 1101 011 0010 111 011 0010 111 100 1101 101 111 100 101 00111 \
104
- # 100 111 01000 01001 000 100 01010 011 000 101 01011 111 011 000 111 \
105
- # 100 1101 101 111 11000 011 11001 101
106
- # None
99
+ >>> huffman(file_path)
100
+ Huffman Coding of file_to_read.txt:
101
+ 00110 1101 011 0010 111 011 0010 111 100 1101 101 111 100 101 00111 \
102
+ 100 111 01000 01001 000 100 01010 011 000 101 01011 111 011 000 111 \
103
+ 100 1101 101 111 11000 011 11001 101
104
+ None
107
105
108
106
"""
109
107
letters_list = parse_file (file_path )
@@ -123,4 +121,4 @@ def huffman(file_path: str) -> None:
123
121
124
122
if __name__ == "__main__" :
125
123
# pass the file path to the huffman function
126
- huffman (sys . argv [ 0 ] )
124
+ huffman ("file_to_read.txt" )
0 commit comments