Skip to content

Commit

Permalink
add exception in Huffman decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
falcondai committed Feb 26, 2019
1 parent 56423f0 commit edf31f4
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions huffman.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,21 @@ def decode(code_tree, encoded):
while not (len(codes) == 0 and type(state) is tuple):
if type(state) is tuple:
# An internal node
code = codes.pop(0)
left, right = state
try:
code = codes.pop(0)
except IndexError:
raise Exception('Decoder should stop at the end of the encoded string. The string may not be encoded by the specified Huffman coding.')
if code == 'l':
# Go left
state = state[0]
state = left
else:
# Go right
state = state[1]
state = right
else:
# A leaf node, decode a letter
decoded.append(state)
# Reset state
# Reset decoder state
state = code_tree
return decoded

Expand Down

0 comments on commit edf31f4

Please sign in to comment.