-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.py
More file actions
26 lines (16 loc) · 742 Bytes
/
decoder.py
File metadata and controls
26 lines (16 loc) · 742 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
def encode_caesar(s: str, offset=1):
''' encode a string using a caesar cipher '''
return ''.join([chr(encode_letter_code(c, offset)) for c in s.lower()])
def encode_letter_code(s: str, offset: int) -> int:
if not s.isalpha():
return ord(s)
return (ord(s) + offset - ord('a')) % 26 + ord('a')
print(encode_caesar('a what is for lunch!', 13))
def decode_caesar(s: str, offset=1):
''' decode a string using a caesar cipher '''
return ''.join([chr(decode_letter_code(c, offset)) for c in s.lower()])
def decode_letter_code(s: str, offset: int) -> int:
if not s.isalpha():
return ord(s)
return (ord(s) - offset - ord('a')) % 26 + ord('a')
print(decode_caesar('n jung vf sbe yhapu!', 13))