From e1bc066ef3e772f0b224b73233f3de7d7cd6bbaa Mon Sep 17 00:00:00 2001 From: Matthew Grove Date: Fri, 21 Feb 2020 16:03:26 +0000 Subject: [PATCH] Fix bug Each character code was prefixed with 0 --- huffman-coding.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/huffman-coding.py b/huffman-coding.py index 9226c55..0ed19a3 100644 --- a/huffman-coding.py +++ b/huffman-coding.py @@ -25,7 +25,7 @@ txt = input("Text: ") info = Counter(txt).most_common() # change the list into ascending order info.reverse() -# create list for character tuples +# create list for character tupl es nodes = [] # create list for node usage frequencies frequencies = [] @@ -36,7 +36,7 @@ for item in info: frequencies.append(item[1]) # repeat until only one top-level node exists -while len(nodes) > 1: +while len(nodes) > 2: # combine two least frequent characters' nodes into a new tuple node, containing the old nodes (old_node_1, old_node_2) new_node = (nodes[0], nodes[1]) # combine two least frequent characters' frequencies into a total frequency, to be used at the top level of the list of nodes