-
-
Save bloomonkey/5574743 to your computer and use it in GitHub Desktop.
Code Breaking. PyPool attempts to solve programming challenge:
http://pi3.sites.sheffield.ac.uk/mini-challenges/chal6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Code Breaking. | |
PyPool attempts to solve programming challenge: | |
http://pi3.sites.sheffield.ac.uk/mini-challenges/chal6 | |
""" | |
import string | |
# Encoded sentences | |
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." | |
sentence3 = "qgamznyhntzjzlvauxjzqgfhfxebjzrm" | |
# Problem 1 | |
def decypher_caeser(sentence, shift): | |
"Decrypt Caesar cypher encrypted `sentence` based on `shift`." | |
# Create strings of shifted alpha characters | |
shifted_upper = string.uppercase[shift:] + string.uppercase[:shift] | |
shifted_lower = string.lowercase[shift:] + string.lowercase[:shift] | |
# Create translation tables | |
# Much more efficient than repeated use of `sentence.replace(a, b)` | |
# Also avoids over-writing already decryted characters | |
upper_table = string.maketrans(string.uppercase, shifted_upper) | |
lower_table = string.maketrans(string.lowercase, shifted_lower) | |
# Chain the two translations | |
# We can do this because the output of the method is also a string | |
decoded = sentence.translate(upper_table).translate(lower_table) | |
return decoded | |
def brute_force(sentence): | |
"""Attempt to brute-force a simple Caesar cypher in `sentence`. | |
""" | |
for shift in range(1, 26): | |
decoded = decypher_caeser(sentence, shift) | |
# Check for some really common words | |
if ' a ' in decoded or ' the ' in decoded: | |
# Print the shift and the decoded line | |
print shift, decoded | |
# Add an extra newline for output readability | |
print '\n' | |
brute_force(sentence1) | |
# Problem 2 | |
sentence2 = sentence2.lower() | |
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) + '\n' | |
# Problem 3 | |
# Pi to 100 digits pasted from Wikipedia | |
pistring = "3.14159 26535 89793 23846 26433 83279 50288 41971 69399 37510 58209 74944 59230 78164 06286 20899 86280 34825 34211 70679".replace('.', '').replace(' ', '') | |
newchars = [] | |
for i, char in enumerate(sentence3): | |
shift = int(pistring[i % len(pistring)]) | |
if char.isalpha(): | |
newchars.append(decypher_caeser(char, shift)) | |
else: | |
newchars.append(char) | |
print ''.join(newchars) + '\n' | |
#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