Skip to content

Commit c47c1ab

Browse files
nikolasvargaspoyea
authored andcommitted
enhancement (#803)
1 parent 76061ab commit c47c1ab

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

‎compression/huffman.py‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __init__(self, freq, left, right):
1818

1919

2020
defparse_file(file_path):
21-
"""
21+
"""
2222
Read the file and build a dict of all letters and their
2323
frequences, then convert the dict into a list of Letters.
2424
"""
@@ -29,15 +29,10 @@ def parse_file(file_path):
2929
ifnotc:
3030
break
3131
chars[c] =chars[c] +1ifcinchars.keys() else1
32-
letters= []
33-
forchar, freqinchars.items():
34-
letter=Letter(char, freq)
35-
letters.append(letter)
36-
letters.sort(key=lambdal: l.freq)
37-
returnletters
32+
returnsorted([Letter(c, f) forc, finchars.items()], key=lambdal: l.freq)
3833

3934
defbuild_tree(letters):
40-
"""
35+
"""
4136
Run through the list of Letters and build the min heap
4237
for the Huffman Tree.
4338
"""
@@ -51,7 +46,7 @@ def build_tree(letters):
5146
returnletters[0]
5247

5348
deftraverse_tree(root, bitstring):
54-
"""
49+
"""
5550
Recursively traverse the Huffman Tree to set each
5651
Letter's bitstring, and return the list of Letters
5752
"""
@@ -64,9 +59,9 @@ def traverse_tree(root, bitstring):
6459
returnletters
6560

6661
defhuffman(file_path):
67-
"""
62+
"""
6863
Parse the file, build the tree, then run through the file
69-
again, using the list of Letters to find and print out the
64+
again, using the list of Letters to find and print out the
7065
bitstring for each letter.
7166
"""
7267
letters_list=parse_file(file_path)

0 commit comments

Comments
(0)