-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession_4.py
91 lines (64 loc) · 2.55 KB
/
session_4.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import string
import math
def caesar_encryption(string, key):
encrypted_str = ""
alphabet = "abcdefghijklmnopqrstuvwxyz"
for char in string:
pos = alphabet.find(char.lower())
if (pos != -1):
new_pos = (pos + key) % len(alphabet)
if char == char.lower():
encrypted_str += alphabet[new_pos]
else:
encrypted_str += alphabet[new_pos].upper()
else:
encrypted_str += char
return encrypted_str
def caesar_decryption(string, key):
decrypted_str = ""
alphabet = "abcdefghijklmnopqrstuvwxyz"
for char in string:
pos = alphabet.find(char.lower())
if (pos != -1):
new_pos = (pos - key) % len(alphabet)
if char == char.lower():
decrypted_str += alphabet[new_pos]
else:
decrypted_str += alphabet[new_pos].upper()
else:
decrypted_str += char
return decrypted_str
def caesar_encryption_decryption(string, key, flag):
final_str = ""
alphabet = "abcdefghijklmnopqrstuvwxyz"
for char in string:
pos = alphabet.find(char.lower())
if (pos != -1):
if flag:
new_pos = (pos + key) % len(alphabet)
else:
new_pos = (pos - key) % len(alphabet)
if char == char.lower():
final_str += alphabet[new_pos]
else:
final_str += alphabet[new_pos].upper()
else:
final_str += char
return final_str
def test_caesar_encryption():
assert caesar_encryption('abc', 3) == "def"
assert caesar_encryption('xyz', 3) == "abc"
assert caesar_encryption('DFi', 3) == "GIl"
assert caesar_encryption('abc d', 3) == "def g"
def test_caesar_decryption():
assert caesar_decryption('def', 3) == "abc"
assert caesar_decryption('abc', 3) == "xyz"
assert caesar_decryption('GIl', 3) == "DFi"
assert caesar_decryption('def g', 3) == "abc d"
def test_caesar_encryption_decryption():
assert caesar_encryption_decryption('def', 3, False) == "abc"
assert caesar_encryption_decryption('abc', 3, False) == "xyz"
assert caesar_encryption_decryption('GIl', 3, False) == "DFi"
assert caesar_encryption_decryption('abc', 3, True) == "def"
assert caesar_encryption_decryption('xyz', 3, True) == "abc"
assert caesar_encryption_decryption('DFi', 3, True) == "GIl"