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

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

1919

2020
def parse_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
if not c:
3030
break
3131
chars[c] = chars[c] + 1 if c in chars.keys() else 1
32-
letters = []
33-
for char, freq in chars.items():
34-
letter = Letter(char, freq)
35-
letters.append(letter)
36-
letters.sort(key=lambda l: l.freq)
37-
return letters
32+
return sorted([Letter(c, f) for c, f in chars.items()], key=lambda l: l.freq)
3833

3934
def build_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
return letters[0]
5247

5348
def traverse_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
return letters
6560

6661
def huffman(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)