-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar.py
More file actions
executable file
·71 lines (56 loc) · 2.08 KB
/
caesar.py
File metadata and controls
executable file
·71 lines (56 loc) · 2.08 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
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
#!/usr/bin/env python3
""" Caesar Cipher
A caesar cipher is a simple substitution cipher where each letter of the
plain text is substituted with a letter found by moving 'n' places down the
alphabet. For an example, if the input plain text is:
abcd xyz
and the shift value, n, is 4. The encrypted text would be:
efgh bcd
You are to write a function which accepts two arguments, a plain-text
message and a number of letters to shift in the cipher. The function will
return an encrypted string with all letters being transformed while all
punctuation and whitespace remains unchanged.
Note: You can assume the plain text is all lowercase ascii, except for
whitespace and punctuation.
"""
import unittest
def caesar(plain_text, shift_num=1):
# TODO: Your code goes here!
result = plain_text
return result
class CaesarTestCase(unittest.TestCase):
def test_a(self):
start = "aaa"
result = caesar(start, 1)
self.assertEqual(result, "bbb")
result = caesar(start, 5)
self.assertEqual(result, "fff")
def test_punctuation(self):
start = "aaa.bbb"
result = caesar(start, 1)
self.assertEqual(result, "bbb.ccc")
result = caesar(start, -1)
self.assertEqual(result, "zzz.aaa")
def test_whitespace(self):
start = "aaa bb b"
result = caesar(start, 1)
self.assertEqual(result, "bbb cc c")
result = caesar(start, 3)
self.assertEqual(result, "ddd ee e")
def test_wraparound(self):
start = "abc"
result = caesar(start, -1)
self.assertEqual(result, "zab")
result = caesar(start, -2)
self.assertEqual(result, "yza")
result = caesar(start, -3)
self.assertEqual(result, "xyz")
start = "xyz"
result = caesar(start, 1)
self.assertEqual(result, "yza")
result = caesar(start, 2)
self.assertEqual(result, "zab")
result = caesar(start, 3)
self.assertEqual(result, "abc")
if __name__ == "__main__":
unittest.main()