-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecrypt.py
More file actions
50 lines (39 loc) · 1.12 KB
/
decrypt.py
File metadata and controls
50 lines (39 loc) · 1.12 KB
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
45
46
47
48
49
50
'''
Example:
("bdfhjacegi", 1) -> "abcdefghij"
("dhaeibfjcg", 2) -> "bdfhjacegi" -> "abcdefghij"
'''
def decrypt(encrypted_text: list, n: int):
if encrypted_text == '':
return encrypted_text
if encrypted_text is None:
return encrypted_text
if (n == '') or (int(n) <= 0):
return encrypted_text
encrypted_text = list(encrypted_text)
text_len = len(encrypted_text)
if text_len % 2 == 0:
half_of_text_len = int(text_len / 2)
else:
half_of_text_len = int((text_len / 2) + 1)
result = ''
i = 0
n = int(n)
try:
while i <= half_of_text_len:
result += ''.join(encrypted_text[half_of_text_len + i])
if i == half_of_text_len:
return result
else:
result += ''.join(encrypted_text[0 + i])
i += 1
return result
except IndexError:
return result
finally:
if n > 1:
return decrypt(result, n - 1)
return result
encrypted_text = input('Type your encrypted message: ')
n = input('Type n param: ')
print(decrypt(encrypted_text, n))