Talk:Huffman coding: Difference between revisions

→‎IS This OK?: Change sort order, larger example
(→‎IS This OK?: Change sort order, larger example)
Line 114:
if tutor: print " PRODUCING:", lohi, '\n'
heappush(heap, lohi)
return sorted(heappop(heap)[1:], key=lambda x: int(len(x[-1]),2 x))
 
#readin = "B 25 C 2.5 D 12.5 A 5 \n"
Line 149:
C 12.5 110
D 12.5 111</pre>
 
With the following lines inserted into the program:
<lang python>astring = "this is an example of a huffman tree"
symbol2weights = dict((ch, astring.count(ch)) for ch in set(astring))
</lang>
I get the following codes:
<pre>SYMBOL WEIGHT HUFFMAN CODE
7 111
a 4 000
e 4 001
f 3 1101
h 2 0100
i 2 0101
m 2 0111
n 2 1000
s 2 1010
t 2 1011
l 1 01100
o 1 01101
p 1 10010
r 1 10011
u 1 11000
x 1 11001</pre>
Anonymous user