Skip to content

Instantly share code, notes, and snippets.

@dotapetro
Last active June 27, 2017 15:15
Show Gist options
  • Save dotapetro/12afa1236456e0251583ca2908ea42cc to your computer and use it in GitHub Desktop.
Save dotapetro/12afa1236456e0251583ca2908ea42cc to your computer and use it in GitHub Desktop.
Just another one realization of Huffman Code
from math import floor
what_to_zip = '''
Scene: A cafe. One table is occupied by a group of Vikings with horned helmets on. A man and his wife enter.
Man (Eric Idle): You sit here, dear.
Wife (Graham Chapman in drag): All right.
Man (to Waitress): Morning!
Waitress (Terry Jones, in drag as a bit of a rat-bag): Morning!
Man: Well, what've you got?
Waitress: Well, there's egg and bacon; egg sausage and bacon; egg and spam; egg bacon and spam; egg bacon sausage and spam; spam bacon sausage and spam; spam egg spam spam bacon and spam; spam sausage spam spam bacon spam tomato and spam;
Vikings (starting to chant): Spam spam spam spam...
Waitress: ...spam spam spam egg and spam; spam spam spam spam spam spam baked beans spam spam spam...
Vikings (singing): Spam! Lovely spam! Lovely spam!
Waitress: ...or Lobster Thermidor au Crevette with a Mornay sauce served in a Provencale manner with shallots and aubergines garnished with truffle pate, brandy and with a fried egg on top and spam.
Wife: Have you got anything without spam?
Waitress: Well, there's spam egg sausage and spam, that's not got much spam in it.
Wife: I don't want ANY spam!
Man: Why can't she have egg bacon spam and sausage?
Wife: THAT'S got spam in it!
Man: Hasn't got as much spam in it as spam egg sausage and spam, has it?
Vikings: Spam spam spam spam (crescendo through next few lines)
Wife: Could you do the egg bacon spam and sausage without the spam then?
Waitress: Urgghh!
Wife: What do you mean 'Urgghh'? I don't like spam!
Vikings: Lovely spam! Wonderful spam!
Waitress: Shut up!
Vikings: Lovely spam! Wonderful spam!
Waitress: Shut up! (Vikings stop) Bloody Vikings! You can't have egg bacon spam and sausage without the spam.
Wife (shrieks): I don't like spam!
Man: Sshh, dear, don't cause a fuss. I'll have your spam. I love it. I'm having spam spam spam spam spam spam spam beaked beans spam spam spam and spam!
Vikings (singing): Spam spam spam spam. Lovely spam! Wonderful spam!
Waitress: Shut up!! Baked beans are off.
Man: Well could I have her spam instead of the baked beans then?
Waitress: You mean spam spam spam spam spam spam... (but it is too late and the Vikings drown her words)
Vikings (singing elaborately): Spam spam spam spam. Lovely spam! Wonderful spam! Spam spa-a-a-a-a-am spam spa-a-a-a-a-am spam. Lovely spam! Lovely spam! Lovely spam! Lovely spam! Lovely spam! Spam spam spam spam!
'''
class HuffmanCode:
def __init__(self):
self.array = [None]
def get_enum(self, text):
self.array = [None]
end = [ord(letter) for letter in text]
end = sorted({i: end.count(i) for i in end}.items(), key=lambda x: x[1])[::-1]
end = dict((x, y) for x, y in end)
self.array.extend(end.keys())
def parent_index(self, i):
return int(floor((i-1)/2))
def get_char_for_str(self, str):
instuctions = [int(i) for i in str]
curr = instuctions.pop(0) - 1
for i in instuctions:
if i == 1:
curr = curr * 2 + 2
else:
curr = curr * 2 + 1
return chr(self.array[curr])
def get_str_for_char(self, char):
char = ord(char)
index = self.array.index(char)
instruction = ''
if index == -1:
return -1
while self.array[index] is not None:
if index % 2 != 0:
instruction += '0'
else:
instruction += '1'
index = self.parent_index(index)
return '1' + instruction[::-1]
def encrypt(self, text):
self.get_enum(text)
new_text = ''
for let in text:
new_text += chr(int(self.get_str_for_char(let), 2))
return new_text
def decrypt(self, text):
decrypted = ''
for let in text:
decrypted += self.get_char_for_str(str(bin(ord(let)))[2:])
return decrypted
H = HuffmanCode()
enc = H.encrypt(what_to_zip)
dec = H.decrypt(enc)
enc_ords = [ord(let) for let in enc]
dec_ords = [ord(let) for let in dec]
print(enc)
print('\n\n DECRYPTED\n\n')
print(dec)
print('win:', 'decrypted ord sum size:', sum(dec_ords), '|encrypted ord sum size:', sum(enc_ords), '|difference:',
sum(dec_ords) - sum(enc_ords), '|percents:', (sum(dec_ords) - sum(enc_ords)) / sum(dec_ords))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment