@@ -0,0 +1,993 @@
+from __future__ import print_function%0A%0A#from heapq import heappush, heappop, heapify%0Aimport heapq%0Afrom collections import defaultdict%0A%0Adef encode(frequency):%0A    heap = %5B%5Bweight, %5Bsymbol, %22%22%5D%5D for symbol, weight in frequency.items()%5D%0A    heapq.heapify(heap)%0A    while len(heap) %3E 1:%0A        lo = heapq.heappop(heap)%0A        hi = heapq.heappop(heap)%0A        for pair in lo%5B1:%5D:%0A            pair%5B1%5D = '0' + pair%5B1%5D%0A        for pair in hi%5B1:%5D:%0A            pair%5B1%5D = '1' + pair%5B1%5D%0A        heapq.heappush(heap, %5Blo%5B0%5D+hi%5B0%5D%5D + lo%5B1:%5D+hi%5B1:%5D)%0A    return sorted(heapq.heappop(heap)%5B1:%5D, key=lambda p: (len(p%5B-1%5D), p))%0A%0Aif __name__ == '__main__':%0A    data = %22The frog at the bottom of the well drifts off into the great ocean%22%0A    frequency = defaultdict(int)%0A    for symbol in data:%0A        frequency%5Bsymbol%5D += 1%0A    huff = encode(frequency)%0A    print(%22Symbol%22.ljust(10) + %22Weight%22.ljust(10) + %22Huffman Code%22)%0A    for p in huff:%0A        print(p%5B0%5D.ljust(10) + str(frequency%5Bp%5B0%5D%5D).ljust(10) + p%5B1%5D)%0A

add huffman encoding