-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotational_cipher.py
More file actions
37 lines (28 loc) · 835 Bytes
/
rotational_cipher.py
File metadata and controls
37 lines (28 loc) · 835 Bytes
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
"""
rotational-cipher
"""
def cipher(char, key):
"""
sums the key using % 26
"""
lower = ord("a")
upper = ord("A")
start = upper
if ord(char) >= lower:
start = lower
if ord(char) >= upper:
return chr((ord(char) + key - start) % 26 + start)
return char
def rotate(text, key):
"""
a simple shift cipher that relies on transposing all the letters in the alphabet
using an integer key between `0` and `26`
an elegant solution would be:
rot = ascii_lowercase[key:] + ascii_lowercase[:key]
return chars.translate(str.maketrans(ascii_lowercase +
ascii_lowercase.upper(), rot + rot.upper()))
"""
text_list = list(text)
for index, char in enumerate(text_list):
text_list[index] = cipher(char, key)
return "".join(text_list)