-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcipher.py
More file actions
45 lines (34 loc) · 1.07 KB
/
cipher.py
File metadata and controls
45 lines (34 loc) · 1.07 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
"""
Implement a simple rotations cypher
"""
map1 = "abcdefghijklmnopqrstuvwxyz"
map2 = "etaoinshrdlucmfwypvbgkjqxz"
class Cipher(object):
def __init__(self, map1, map2):
self.inmap = map1
self.outmap = map2
def code(self, string,frommap,tomap):
r = ''
for s in string:
try:
r += tomap[frommap.index(s)]
except (ValueError, KeyError):
r += s
return r
def encode(self, string):
return self.code(string=string,
frommap=self.inmap,
tomap=self.outmap
)
def decode(self, string):
return self.code(string=string,
frommap=self.outmap,
tomap=self.inmap
)
import unittest
class TestFirst(unittest.TestCase):
def test_first(self):
map1 = "abcdefghijklmnopqrstuvwxyz"
map2 = "etaoinshrdlucmfwypvbgkjqxz"
cipher = Cipher(map1, map2)
self.assertEqual(cipher.encode("abc"), "eta")