Skip to content

Commit 9dcc06f

Browse files
Julianleios
Julian
andauthored
fix both error of issue 739 (#756)
* fix both error of issue 739 * Update contents/huffman_encoding/code/python/huffman.py Co-authored-by: James Schloss <[email protected]>
1 parent a4dbc2a commit 9dcc06f

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

Diff for: contents/huffman_encoding/code/python/huffman.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,17 @@ def build_huffman_tree(message):
2929
# insert the new tree there
3030
trees.insert(index, (new_tree, new_weight))
3131

32-
huffman_tree = trees[0][0]
32+
# Return an empty list if the message was empty, else the tree
33+
huffman_tree = [] if not trees else trees[0][0]
3334
return huffman_tree
3435

3536
# constructs the mapping with recursion
3637
def build_codebook(tree, code=''):
38+
# Check whether our tree contains more than 1 element or not
39+
if not tree:
40+
return []
41+
elif type(tree) != list:
42+
return [(tree, '0')]
3743

3844
codebook = []
3945

0 commit comments

Comments
 (0)