1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| class Node(): def __init__(self, name=None, value=None): self.name = name self.value = value self.right = None self.left = None
class HuffmanTree(): def __init__(self, arr): self.charset = {} self.nodes = [Node(values[0], values[1]) for values in arr] while len(self.nodes) != 1: self.nodes.sort(key=lambda node: node.value) p = Node(value=(self.nodes[0].value + self.nodes[1].value)) p.left = self.nodes.pop(0) p.right = self.nodes.pop(0) self.nodes.append(p) self.root = self.nodes[0]
self.Code = list(range(60))
def HuffmanCode(self, tree, length): node = tree if (not node): return elif node.name: result = '' for i in range(length): result += str(self.Code[i]) self.charset[node.name] = result return self.Code[length] = 0 self.HuffmanCode(tree.left, length + 1) self.Code[length] = 1 self.HuffmanCode(tree.right, length+1)
def GenCode(self): self.HuffmanCode(self.root, 0)
alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789!@#%^&*()_+-=[]{};' arr = [] for i in range(54): arr.append((43*(127+i)) % 233) char_weights = list(zip(alphabet, arr)) tree = HuffmanTree(char_weights) tree.GenCode()
charset = { 'b': '00001', 'y': '10011', 't': '11000', 'e': '0011010', 'c': '01110', 'f': '010010', '{': '10001', 'a': '100101', 'd': '11011', 'g': '111011', 'h': '01000', 'i': '10110', 'j': '00110111', 'k': '1111010', 'l': '110010', 'm': '00011', 'n': '10000', 'o': '10100011101', 'p': '0110011', 'q': '011000', 'r': '111110', 's': '01011', 'u': '11110110', 'v': '000001', 'w': '111000', 'x': '00101', 'z': '101000110', '0': '1110010', '1': '100100', '2': '111111', '3': '01101', '4': '11010', '5': '11110111', '}': '1010001111', '6': '001100', '7': '111010', '8': '00111', '9': '10101', '!': '00110110', '@': '1110011', '#': '101001', '%': '00010', '^': '01111', '&': '10100011100', '*': '0110010', '(': '010011', ')': '111100', '_': '01010', '+': '10111', '-': '10100010', '=': '000000', '[': '110011', ']': '00100', ';': '1010000'
}
assert(charset == tree.charset)
new_dic = dict(zip(tree.charset.values(), tree.charset.keys()))
target = [0x0c, 0xf0, 0x69, 0xd8, 0x4a, 0x32, 0xfb, 0x62, 0x8e, 0xa4, 0xcc, 0x0c, 0xc0, 0x22, 0x63, 0xe5, 0xb6, 0xfd, 0x07, 0x5e, 0xe6, 0xfe, 0xc6, 0x8d, 0xfd, 0x8d, 0x51, 0xad, 0xe4, 0x68, 0xfa, 0x14, 0x78] f = ''.join([bin(i)[2:].rjust(8, '0') for i in target])
i = 0 j = 5 while j < len(f): tmp = f[i:j] if new_dic.get(tmp): print(new_dic[tmp], end='') i = j j += 5 else: j += 1
|
Comments