-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdecrypt_message.py
44 lines (32 loc) · 1.19 KB
/
decrypt_message.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
Decrypt Message
Every word is encrypted as follows:
- Convert every letter to its ASCII value.
- Add 1 to the first letter, and then for every letter from the second one to the last one, add the value of the previous letter.
- Subtract 26 from every letter until it is in the range of lowercase letters a-z in ASCII.
- Convert the values back to letters.
For instance, to encrypt the word “crime”
Decrypted message: c r i m e
Step 1: 99 114 105 109 101
Step 2: 100 214 319 428 529
Step 3: 100 110 111 116 113
Encrypted message: d n o t q
Write a function named decrypt(word) that receives a string that consists of small latin letters only, and returns the decrypted word.
input: word = "dnotq"
output: "crime"
input: word = "flgxswdliefy"
output: "encyclopedia"
"""
# O(n) time
# O(n) space
def decrypt(word):
decryption = ""
prev_letter_val = 1
for letter in word:
letter_ascii_val = ord(letter)
letter_ascii_val -= prev_letter_val
while letter_ascii_val < ord('a'):
letter_ascii_val += 26
decryption += chr(letter_ascii_val)
prev_letter_val += letter_ascii_val
return decryption