Skip to content

Instantly share code, notes, and snippets.

@go-cristian
Created March 1, 2020 04:54
Show Gist options
  • Save go-cristian/443b669eff10263070187cc2ec7855f3 to your computer and use it in GitHub Desktop.
Save go-cristian/443b669eff10263070187cc2ec7855f3 to your computer and use it in GitHub Desktop.
# Crack the code, and write a function to decode
# any new messages you receive from mystery envelopes.
def puzzle(encoded_message):
encoded_lowercase = encoded_message.lower()
plaintext = ''
for i in range(len(encoded_lowercase)):
ascii = ord(encoded_lowercase[i])
if ascii >= 97 and ascii<=122:
new_value = ascii - i
while new_value < 97:
new_value = 122 - (96 - new_value)
plaintext += chr(new_value)
else:
plaintext += encoded_lowercase[i]
return plaintext
### Tests
secret_messages = (
"Ig Bsz swEo IrpHEtCxNN xLC ELDRLU, OSYP ocTiWp Bkauhn.",
"Rfcg fhvCC DAzs MrQL OK GLPSQYI eVca eMYYh Rl yheiw MpthltAzm'C nyCv.",
"Fftqesjv YoCqM HJrJM xKKRQICWWMSM bX TBlhWee gcia wdasu elu.",
"Lbvh fz vrqsF WCuJ fIIPxLH ICFOX HeJi aa iefdl ako Zrukimhoht syFrzAyxwGwz.",
"Ig Bsz DiwD FB vuK LNvNQCC JP QFIOQWO XROgdZfZ, pecqe kv f iwxu IEwIJvF vT pyQAI jYNJV fYcfU gURVbhb.",
"Wjvk ynl ykypnG FIGCyxP, BRFCWII Ig GPe ARAZfgYt, wnu edr qlmy JAHF trLt DJ ANLVOQW.",
"BfcxxnlBt sD osIJvJ NCwK TGMa.",
"EyromhoA rC nrHIuI MBvJ GLPMKFMY.",
"Sjospj pA lpFGsG KztH yLKOLFZ.",
"CpospjD qB mqGHtH LAuI zMLPMKFEYKK.",
"Fmcw ny jnDEqE IxrF HzOQCC.",
"Sqcuwj pA lpFGsG KztH zBLRE.",
"RfcgegosqCI oBICJJ.",
)
for message in secret_messages:
print(puzzle(message))
print('---')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment