forked from stefoxp/codewars
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleEncryption01AlternatingSplit_6kyu.py
More file actions
68 lines (45 loc) · 1.3 KB
/
SimpleEncryption01AlternatingSplit_6kyu.py
File metadata and controls
68 lines (45 loc) · 1.3 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
For building the encrypted string:
Take every 2nd char from the string,
then the other chars, that are not every 2nd char,
and concat them as new String.
Do this n times!
Examples:
"This is a test!", 1 -> "hsi etTi sats!"
"This is a test!", 2 -> "hsi etTi sats!" -> "s eT ashi tist!"
Write two methods:
def encrypt(text, n)
def decrypt(encrypted_text, n)
For both methods:
If the input-string is null or empty return exactly this value!
If n is <= 0 then return the input text.
"""
def decrypt(encrypted_text, n):
result = encrypted_text
if n > 0:
for num in range(n):
result = de_substitute(list(result))
return result
def de_substitute(encrypted_text):
sx = encrypted_text[:int(len(encrypted_text) / 2)]
result = encrypted_text[int(len(encrypted_text) / 2):]
offset = 0
for i in range(len(encrypted_text)):
if i % 2 != 0:
result.insert(i, sx[offset])
offset += 1
return ''.join(result)
def encrypt(text, n):
result = text
if n > 0:
for num in range(n):
result = substitute(list(result))
return result
def substitute(text):
result = ""
for i in range(1, len(text), 2):
result += text[i]
text[i] = ""
for x in text:
result += x
return result