Skip to content

Commit dfeffff

Browse files
committed
Started working on the rotational-cipher challenge
1 parent 2cb6282 commit dfeffff

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"track":"python","exercise":"rotational-cipher","id":"0fda01dc4249444eb8e43fe516773f34","url":"https://exercism.io/my/solutions/0fda01dc4249444eb8e43fe516773f34","handle":"sinanata","is_requester":true,"auto_approve":false}

rotational-cipher/README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Rotational Cipher
2+
3+
Create an implementation of the rotational cipher, also sometimes called the Caesar cipher.
4+
5+
The Caesar cipher is a simple shift cipher that relies on
6+
transposing all the letters in the alphabet using an integer key
7+
between `0` and `26`. Using a key of `0` or `26` will always yield
8+
the same output due to modular arithmetic. The letter is shifted
9+
for as many values as the value of the key.
10+
11+
The general notation for rotational ciphers is `ROT + <key>`.
12+
The most commonly used rotational cipher is `ROT13`.
13+
14+
A `ROT13` on the Latin alphabet would be as follows:
15+
16+
```text
17+
Plain: abcdefghijklmnopqrstuvwxyz
18+
Cipher: nopqrstuvwxyzabcdefghijklm
19+
```
20+
21+
It is stronger than the Atbash cipher because it has 27 possible keys, and 25 usable keys.
22+
23+
Ciphertext is written out in the same formatting as the input including spaces and punctuation.
24+
25+
## Examples
26+
27+
- ROT5 `omg` gives `trl`
28+
- ROT0 `c` gives `c`
29+
- ROT26 `Cool` gives `Cool`
30+
- ROT13 `The quick brown fox jumps over the lazy dog.` gives `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.`
31+
- ROT13 `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.` gives `The quick brown fox jumps over the lazy dog.`
32+
33+
34+
## Exception messages
35+
36+
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
37+
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
38+
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
39+
a message.
40+
41+
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
42+
`raise Exception`, you should write:
43+
44+
```python
45+
raise Exception("Meaningful message indicating the source of the error")
46+
```
47+
48+
## Running the tests
49+
50+
To run the tests, run `pytest rotational_cipher_test.py`
51+
52+
Alternatively, you can tell Python to run the pytest module:
53+
`python -m pytest rotational_cipher_test.py`
54+
55+
### Common `pytest` options
56+
57+
- `-v` : enable verbose output
58+
- `-x` : stop running tests on first failure
59+
- `--ff` : run failures from previous test before running other test cases
60+
61+
For other options, see `python -m pytest -h`
62+
63+
## Submitting Exercises
64+
65+
Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/rotational-cipher` directory.
66+
67+
You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`.
68+
69+
For more detailed information about running tests, code style and linting,
70+
please see [Running the Tests](http://exercism.io/tracks/python/tests).
71+
72+
## Source
73+
74+
Wikipedia [https://en.wikipedia.org/wiki/Caesar_cipher](https://en.wikipedia.org/wiki/Caesar_cipher)
75+
76+
## Submitting Incomplete Solutions
77+
78+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def rotate(text, key):
2+
pass
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import unittest
2+
3+
from rotational_cipher import rotate
4+
5+
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.2.0
6+
7+
8+
class RotationalCipherTest(unittest.TestCase):
9+
def test_rotate_a_by_0_same_output_as_input(self):
10+
self.assertEqual(rotate("a", 0), "a")
11+
12+
def test_rotate_a_by_1(self):
13+
self.assertEqual(rotate("a", 1), "b")
14+
15+
def test_rotate_a_by_26_same_output_as_input(self):
16+
self.assertEqual(rotate("a", 26), "a")
17+
18+
def test_rotate_m_by_13(self):
19+
self.assertEqual(rotate("m", 13), "z")
20+
21+
def test_rotate_n_by_13_with_wrap_around_alphabet(self):
22+
self.assertEqual(rotate("n", 13), "a")
23+
24+
def test_rotate_capital_letters(self):
25+
self.assertEqual(rotate("OMG", 5), "TRL")
26+
27+
def test_rotate_spaces(self):
28+
self.assertEqual(rotate("O M G", 5), "T R L")
29+
30+
def test_rotate_numbers(self):
31+
self.assertEqual(rotate("Testing 1 2 3 testing", 4), "Xiwxmrk 1 2 3 xiwxmrk")
32+
33+
def test_rotate_punctuation(self):
34+
self.assertEqual(rotate("Let's eat, Grandma!", 21), "Gzo'n zvo, Bmviyhv!")
35+
36+
def test_rotate_all_letters(self):
37+
self.assertEqual(
38+
rotate("The quick brown fox jumps over the lazy dog.", 13),
39+
"Gur dhvpx oebja sbk whzcf bire gur ynml qbt.",
40+
)
41+
42+
43+
if __name__ == "__main__":
44+
unittest.main()

0 commit comments

Comments
 (0)