Return to Snippet

Revision: 48307
at June 28, 2011 22:36 by themoosemind


Initial Code
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)

Initial URL

                                

Initial Description

                                

Initial Title
Decode Bacon\'s cipher

Initial Tags
python

Initial Language
Python