Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created April 21, 2018 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/20b9861d0f423b7d88b44cda2b45fb5e to your computer and use it in GitHub Desktop.
Save jianminchen/20b9861d0f423b7d88b44cda2b45fb5e to your computer and use it in GitHub Desktop.
Decrypt message - being interviewer
def decrypt(word):
"""
c r i m e
99 114 105 109 101 (ascii value)
100 214 319 428 529 (+1 first letter, add previous from 2nd to last one)
100 110 111 116 113 (substract 26 from every letter until it is between ascii('a') and ascii('z'))
0 104 208 312 416
0*26 4*26 8*26 12*26 16*26
a -> 97
z -> 122
a -> a + 1
b -> b + (a + 1)
c -> c + (b + (a + 1))
d -> d + (c + (b + (a + 1)))
- add (4*i)*26 from every letter
- letteri = letteri - letteri-1
- letter0 = letter0 - 1
given by Julia:
formula: Sum(source[i]), encrypt[i] = source[i] + sum(source[0 to i -1]) - 26 * n
// source[i] = encrypt[i] - sum(source[0 to i - 1]) + 26 * n
"""
if not word:
return ""
values = [ord(c) for c in word]
for i in range(len(values) - 1, 0, -1):
values[i] -= values[i - 1] # outofrange a - z
values[0] -= 1
for i in range(len(values)):
while values[i] < ord('a'):
values[i] += 26
message = "".join([chr(n) for n in values])
return message
word = "dnotq"
r = decrypt(word)
print(r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment