-
Notifications
You must be signed in to change notification settings - Fork 0
/
caesar.py
39 lines (29 loc) · 970 Bytes
/
caesar.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
def alphabet_position(character):
alphabet = 'abcdefghijklmnopqrstuvwxyz'
lower = character.lower()
return alphabet.index(lower)
def rotate_string_13(text):
rotated = ''
alphabet = 'abcdefghijklmnopqrstuvwxyz'
for char in text:
rotated_idx = (alphabet_position(char) + 13) % 26
if char.isupper():
rotated = rotated + alphabet[rotated_idx].upper()
else:
rotated = rotated + alphabet[rotated_idx]
return rotated
def rotate_character(char, rot):
alphabet = 'abcdefghijklmnopqrstuvwxyz'
rotated_idx = (alphabet_position(char) + rot) % 26
if char.isupper():
return alphabet[rotated_idx].upper()
else:
return alphabet[rotated_idx]
def rotate_string(text, rot):
rotated = ''
for char in text:
if (char.isalpha()):
rotated = rotated + rotate_character(char, rot)
else:
rotated = rotated + char
return rotated