|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" Caesar Cipher |
| 3 | + A caesar cipher is a simple substitution cipher where each letter of the |
| 4 | + plain text is substituted with a letter found by moving 'n' places down the |
| 5 | + alphabet. For an example, if the input plain text is: |
| 6 | +
|
| 7 | + abcd xyz |
| 8 | +
|
| 9 | + and the shift value, n, is 4. The encrypted text would be: |
| 10 | +
|
| 11 | + efgh bcd |
| 12 | +
|
| 13 | + You are to write a function which accepts two arguments, a plain-text |
| 14 | + message and a number of letters to shift in the cipher. The function will |
| 15 | + return an encrypted string with all letters being transformed while all |
| 16 | + punctuation and whitespace remains unchanged. |
| 17 | +
|
| 18 | + Note: You can assume the plain text is all lowercase ascii, except for |
| 19 | + whitespace and punctuation. |
| 20 | +""" |
| 21 | +import unittest |
| 22 | + |
| 23 | + |
| 24 | +def caesar(plain_text, shift_num=1): |
| 25 | + # TODO: Your code goes here! |
| 26 | + result = plain_text |
| 27 | + return result |
| 28 | + |
| 29 | + |
| 30 | +class CaesarTestCase(unittest.TestCase): |
| 31 | + def test_a(self): |
| 32 | + start = "aaa" |
| 33 | + result = caesar(start, 1) |
| 34 | + self.assertEqual(result, "bbb") |
| 35 | + result = caesar(start, 5) |
| 36 | + self.assertEqual(result, "fff") |
| 37 | + |
| 38 | + def test_punctuation(self): |
| 39 | + start = "aaa.bbb" |
| 40 | + result = caesar(start, 1) |
| 41 | + self.assertEqual(result, "bbb.ccc") |
| 42 | + result = caesar(start, -1) |
| 43 | + self.assertEqual(result, "zzz.aaa") |
| 44 | + |
| 45 | + def test_whitespace(self): |
| 46 | + start = "aaa bb b" |
| 47 | + result = caesar(start, 1) |
| 48 | + self.assertEqual(result, "bbb cc c") |
| 49 | + result = caesar(start, 3) |
| 50 | + self.assertEqual(result, "ddd ee e") |
| 51 | + |
| 52 | + def test_wraparound(self): |
| 53 | + start = "abc" |
| 54 | + result = caesar(start, -1) |
| 55 | + self.assertEqual(result, "zab") |
| 56 | + result = caesar(start, -2) |
| 57 | + self.assertEqual(result, "yza") |
| 58 | + result = caesar(start, -3) |
| 59 | + self.assertEqual(result, "xyz") |
| 60 | + |
| 61 | + start = "xyz" |
| 62 | + result = caesar(start, 1) |
| 63 | + self.assertEqual(result, "yza") |
| 64 | + result = caesar(start, 2) |
| 65 | + self.assertEqual(result, "zab") |
| 66 | + result = caesar(start, 3) |
| 67 | + self.assertEqual(result, "abc") |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == "__main__": |
| 71 | + unittest.main() |
0 commit comments