-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar.py
More file actions
25 lines (19 loc) · 719 Bytes
/
caesar.py
File metadata and controls
25 lines (19 loc) · 719 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
# caesar.py
ALPHABET_LOWERCASE = "abcdefghijklmnopqrstuvwxyz"
ALPHABET_UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def alphabet_position(letter):
alphabet = ALPHABET_LOWERCASE if letter.islower() else ALPHABET_UPPERCASE
return alphabet.index(letter)
def rotate_char(char, rotation):
if not char.isalpha():
return char
alphabet = ALPHABET_LOWERCASE if char.islower() else ALPHABET_UPPERCASE
#print("Char is type:",type(char))
#print("rotation is type:",type(rotation))
new_pos = (alphabet_position(char) + rotation) % 26
return alphabet[new_pos]
def encrypt(text, rotation):
answer = ""
for char in text:
answer += rotate_char(char, rotation)
return answer