-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCaesar-cipher.py
More file actions
46 lines (45 loc) · 1.39 KB
/
Caesar-cipher.py
File metadata and controls
46 lines (45 loc) · 1.39 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
class caesarCipher:
"""
Encode input data by Caesar Cipher.
`@name` encode
`@param` {`string`} text - raw data for encode.
`@param` {`number`} key - (`default 1`) count of shift.
`@return` {`string`} The encoded by Caesar Cipher data
"""
@staticmethod
def encode(text, key=1):
try:
text
if type(text) != str:
throw
text = list(text)
if type(key) is str and key > 1:
key = int(key)
except NameError:
return None
res = []
for el in text:
res.append(chr(ord(el) + key))
return "".join([str(el) for el in res])
"""
Decode input data by Caesar Cipher.
`@name` decode
`@param` {`string`} text - raw data for decode.
`@param` {`number`} key - (`default 1`) count of shift.
`@return` {`string`} The decoded by Caesar Cipher data
"""
@staticmethod
def decode(text, key=1):
try:
text
if type(text) != str:
throw
text = list(text)
if type(key) is str and key > 1:
key = int(key)
except NameError:
return None
res = []
for el in text:
res.append(chr(ord(el) - key))
return "".join([str(el) for el in res])