/ Published in: Python
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
import re def bacon2text(bacon): """ This function converts a string in bacon (a lot of 'a's and 'b's) to plain text. The plaintext is returned. If a character can't be translated, a space will be inserted. """ bacondict = {} plaintext = "" bacon = bacon.lower() bacon = re.sub("[\W\d]", "", bacon.strip()) for i in xrange(0,26): tmp = bin(i)[2:].zfill(5) tmp = tmp.replace('0', 'a') tmp = tmp.replace('1', 'b') bacondict[tmp] = chr(65+i) for i in xrange(0, len(bacon)/5): plaintext = plaintext + bacondict.get(bacon[i*5:i*5+5], ' ') return plaintext crypto = """BAABBAABBBABAAABAABA ABAAABAABA AAAAA AAAABAAAAAAAABAABBBAABBAB AAABAABAAAABBBBAABBBAABAABAAAB ABBAAAABAABAABABAABAAAAAAAABBAAABAA""" print bacon2text(crypto)