mirror of
https://github.com/Xevion/exercism.git
synced 2025-12-06 09:14:59 -06:00
atbash, robot simulator, sum of multiples exercises
This commit is contained in:
1
python/atbash-cipher/.exercism/metadata.json
Normal file
1
python/atbash-cipher/.exercism/metadata.json
Normal file
@@ -0,0 +1 @@
|
||||
{"track":"python","exercise":"atbash-cipher","id":"5a7e43952bd94c4d9a1d698c45748d49","url":"https://exercism.io/my/solutions/5a7e43952bd94c4d9a1d698c45748d49","handle":"Xevion","is_requester":true,"auto_approve":false}
|
||||
78
python/atbash-cipher/README.md
Normal file
78
python/atbash-cipher/README.md
Normal file
@@ -0,0 +1,78 @@
|
||||
# Atbash Cipher
|
||||
|
||||
Create an implementation of the atbash cipher, an ancient encryption system created in the Middle East.
|
||||
|
||||
The Atbash cipher is a simple substitution cipher that relies on
|
||||
transposing all the letters in the alphabet such that the resulting
|
||||
alphabet is backwards. The first letter is replaced with the last
|
||||
letter, the second with the second-last, and so on.
|
||||
|
||||
An Atbash cipher for the Latin alphabet would be as follows:
|
||||
|
||||
```text
|
||||
Plain: abcdefghijklmnopqrstuvwxyz
|
||||
Cipher: zyxwvutsrqponmlkjihgfedcba
|
||||
```
|
||||
|
||||
It is a very weak cipher because it only has one possible key, and it is
|
||||
a simple monoalphabetic substitution cipher. However, this may not have
|
||||
been an issue in the cipher's time.
|
||||
|
||||
Ciphertext is written out in groups of fixed length, the traditional group size
|
||||
being 5 letters, and punctuation is excluded. This is to make it harder to guess
|
||||
things based on word boundaries.
|
||||
|
||||
## Examples
|
||||
|
||||
- Encoding `test` gives `gvhg`
|
||||
- Decoding `gvhg` gives `test`
|
||||
- Decoding `gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt` gives `thequickbrownfoxjumpsoverthelazydog`
|
||||
|
||||
## Exception messages
|
||||
|
||||
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
|
||||
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
|
||||
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
|
||||
a message.
|
||||
|
||||
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
|
||||
`raise Exception`, you should write:
|
||||
|
||||
```python
|
||||
raise Exception("Meaningful message indicating the source of the error")
|
||||
```
|
||||
|
||||
## Running the tests
|
||||
|
||||
To run the tests, run the appropriate command below ([why they are different](https://github.com/pytest-dev/pytest/issues/1629#issue-161422224)):
|
||||
|
||||
- Python 2.7: `py.test atbash_cipher_test.py`
|
||||
- Python 3.4+: `pytest atbash_cipher_test.py`
|
||||
|
||||
Alternatively, you can tell Python to run the pytest module (allowing the same command to be used regardless of Python version):
|
||||
`python -m pytest atbash_cipher_test.py`
|
||||
|
||||
### Common `pytest` options
|
||||
|
||||
- `-v` : enable verbose output
|
||||
- `-x` : stop running tests on first failure
|
||||
- `--ff` : run failures from previous test before running other test cases
|
||||
|
||||
For other options, see `python -m pytest -h`
|
||||
|
||||
## Submitting Exercises
|
||||
|
||||
Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/atbash-cipher` directory.
|
||||
|
||||
You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`.
|
||||
|
||||
For more detailed information about running tests, code style and linting,
|
||||
please see [Running the Tests](http://exercism.io/tracks/python/tests).
|
||||
|
||||
## Source
|
||||
|
||||
Wikipedia [http://en.wikipedia.org/wiki/Atbash](http://en.wikipedia.org/wiki/Atbash)
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
|
||||
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
||||
19
python/atbash-cipher/atbash_cipher.py
Normal file
19
python/atbash-cipher/atbash_cipher.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from string import ascii_letters, punctuation, whitespace
|
||||
|
||||
rev = ascii_letters[25::-1] + ascii_letters[:25:-1]
|
||||
enc = str.maketrans(ascii_letters, rev)
|
||||
dec = str.maketrans(rev, ascii_letters)
|
||||
san = str.maketrans('', '', punctuation)
|
||||
|
||||
def groupings(text, groupby=5):
|
||||
for i in range(0, len(text), groupby):
|
||||
yield text[i:i + groupby]
|
||||
|
||||
def sanitize(text):
|
||||
return ''.join(text.translate(san).split())
|
||||
|
||||
def encode(plain_text):
|
||||
return ' '.join(groupings(sanitize(plain_text.translate(enc).lower())))
|
||||
|
||||
def decode(ciphered_text):
|
||||
return ''.join(ciphered_text.translate(dec).lower().split())
|
||||
69
python/atbash-cipher/atbash_cipher_test.py
Normal file
69
python/atbash-cipher/atbash_cipher_test.py
Normal file
@@ -0,0 +1,69 @@
|
||||
import unittest
|
||||
|
||||
from atbash_cipher import decode, encode
|
||||
|
||||
|
||||
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.2.0
|
||||
|
||||
class AtbashCipherTest(unittest.TestCase):
|
||||
def test_encode_no(self):
|
||||
self.assertMultiLineEqual(encode("no"), "ml")
|
||||
|
||||
def test_encode_yes(self):
|
||||
self.assertMultiLineEqual(encode("yes"), "bvh")
|
||||
|
||||
def test_encode_OMG(self):
|
||||
self.assertMultiLineEqual(encode("OMG"), "lnt")
|
||||
|
||||
def test_encode_O_M_G(self):
|
||||
self.assertMultiLineEqual(encode("O M G"), "lnt")
|
||||
|
||||
def test_encode_long_word(self):
|
||||
self.assertMultiLineEqual(encode("mindblowingly"), "nrmwy oldrm tob")
|
||||
|
||||
def test_encode_numbers(self):
|
||||
self.assertMultiLineEqual(
|
||||
encode("Testing, 1 2 3, testing."), "gvhgr mt123 gvhgr mt")
|
||||
|
||||
def test_encode_sentence(self):
|
||||
self.assertMultiLineEqual(
|
||||
encode("Truth is fiction."), "gifgs rhurx grlm")
|
||||
|
||||
def test_encode_all_things(self):
|
||||
plaintext = "The quick brown fox jumps over the lazy dog."
|
||||
ciphertext = "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"
|
||||
self.assertMultiLineEqual(encode(plaintext), ciphertext)
|
||||
|
||||
def test_decode_word(self):
|
||||
self.assertMultiLineEqual(decode("vcvix rhn"), "exercism")
|
||||
|
||||
def test_decode_sentence(self):
|
||||
self.assertMultiLineEqual(
|
||||
decode("zmlyh gzxov rhlug vmzhg vkkrm thglm v"),
|
||||
"anobstacleisoftenasteppingstone")
|
||||
|
||||
def test_decode_numbers(self):
|
||||
self.assertMultiLineEqual(
|
||||
decode("gvhgr mt123 gvhgr mt"), "testing123testing")
|
||||
|
||||
def test_decode_all_the_letters(self):
|
||||
ciphertext = "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"
|
||||
plaintext = "thequickbrownfoxjumpsoverthelazydog"
|
||||
self.assertMultiLineEqual(decode(ciphertext), plaintext)
|
||||
|
||||
def test_decode_with_too_many_spaces(self):
|
||||
self.assertMultiLineEqual(decode("vc vix r hn"), "exercism")
|
||||
|
||||
def test_decode_with_no_spaces(self):
|
||||
ciphertext = "zmlyhgzxovrhlugvmzhgvkkrmthglmv"
|
||||
plaintext = "anobstacleisoftenasteppingstone"
|
||||
self.assertMultiLineEqual(decode(ciphertext), plaintext)
|
||||
|
||||
# additional track specific test
|
||||
def test_encode_decode(self):
|
||||
self.assertMultiLineEqual(
|
||||
decode(encode("Testing, 1 2 3, testing.")), "testing123testing")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user