Skip to content

Instantly share code, notes, and snippets.

@davidtweaver
Created May 13, 2013 19:10
Show Gist options
  • Save davidtweaver/5570661 to your computer and use it in GitHub Desktop.
Save davidtweaver/5570661 to your computer and use it in GitHub Desktop.
import string
print string.letters
print string.lowercase
sentence1 = "Uijt jt b Dbftbs djqifs. Opx vtf b gsfrvfodz bobmztjt up efdpef uif xfmm-lopxo gjstu qbsbhsbqi, boe mppl gps b tfdsfu nfttbhf bu uif foe"
sentence2 = "Ts tg d swjsu jvtkhwgdmme dfovbpmhnlhn, suds d gtvlmh cdv tv qbgghggtbv ba d lbbn abwsjvh cjgs ih tv pdvs ba d ptah. Ubphkhw mtssmh ovbpv suh ahhmtvlg bw kthpg ba gjfu d cdv cde ih bv utg atwgs hvshwtvl d vhtluibjwubbn, sutg swjsu tg gb phmm atrhn tv suh ctvng ba suh gjwwbjvntvl adctmthg, suds uh tg fbvgtnhwhn dg suh wtlusajm qwbqhwse ba gbch bvh bw bsuhw ba suhtw ndjlushwg. Jgh suh ntltsg ba qt sb gutas suh mhsshwg tv suh vhrs chggdlh abwpdwng.".lower()
sentence3 = "qgamznyhntzjzlvauxjzqgfhfxebjzrm"
def decypher(sentence):
for i in range(1, 26):
table = string.maketrans(string.letters, (string.lowercase[i:] + string.lowercase[:i]) * 2)
de = string.translate(sentence, table)
if ' a ' in de or ' the ' in de:
print i, de
print '\n'
decypher(sentence1)
words = sentence2.split()
wordfreqs = {}
for word in words:
if len(word) < 5:
if word in wordfreqs:
wordfreqs[word] += 1
else:
wordfreqs[word] = 1
cfreqs = {}
for c in sentence2:
if c.isalpha():
if c in cfreqs:
cfreqs[c] += 1
else:
cfreqs[c] = 1
common = "e t a o i n s r h l d c u m f p g w y b v k x j q z".replace(' ', '')
common2 = "e t a o h n i s r d l u w m c g f y p v k b j x z q".replace(' ', '')
common3 = "e t o r i n h s a d l u w m c g f y p v k x j b z q".replace(' ', '')
common3 = ''.join(['e','t', 'i', 's', 'o', 'n', 'h', 'r', 'a', 'f', 'u', 'l', 'd', 'g', 'm', 'w', 'p', 'y', 'c', 'b', 'v', 'k', 'x', 'j', 'q', 'z'])
# Create a decorated list
sortlist = [(value, key) for key, value in wordfreqs.items()]
# Sort it
sortlist.sort(reverse=True)
# Undecorate (print the keys)
for value, key in sortlist[:5]:
print key, '=', value
# Create a decorated list
sortlist = [(value, key) for key, value in cfreqs.items()]
# Sort it
sortlist.sort(reverse=True)
# Undecorate (print the keys)
#for value, key in sortlist[:10]:
# print key, '=', value
ourtextcommon = ''.join([key for value, key in sortlist])
for char in string.lowercase:
if char not in ourtextcommon:
ourtextcommon += char
print ourtextcommon
print ""
table = string.maketrans(ourtextcommon, common[:len(ourtextcommon)])
print string.translate(sentence2, table)
print ""
table2 = string.maketrans(ourtextcommon, common2[:len(ourtextcommon)])
print string.translate(sentence2, table2)
print ""
table3 = string.maketrans(ourtextcommon, common3[:len(ourtextcommon)])
print string.translate(sentence2, table3)
#decodedsentence = []
# for char in sentence1:
# if (char.isalpha()):
# # turn char into a number
# #x = ord(char)
# # move it by 10
# x = string.letters.find(char)
# x = x+i % len(string.letters)#
# #turn new number back into character
# newchar = chr(x)
#put newchar into a new sequence
# decodedsentence.append(newchar)
#
# else:
# decodedsentence.append(char)
# print "".join(decodedsentence) + '\n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment