🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of pfertyk

pfertyk's solution

to Atbash Cipher in the Python Track

Published at Jul 13 2018 · 0 comments
Test suite


This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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:

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.


  • 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:

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):

  • 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 the help page.


Wikipedia 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.


import unittest

from atbash_cipher import decode, encode

# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.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):
            encode("Testing, 1 2 3, testing."), "gvhgr mt123 gvhgr mt")

    def test_encode_sentence(self):
            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):
            decode("zmlyh gzxov rhlug vmzhg vkkrm thglm v"),

    def test_decode_numbers(self):
            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)

    # additional track specific test
    def test_encode_decode(self):
            decode(encode("Testing, 1 2 3, testing.")), "testing123testing")

if __name__ == '__main__':
import string

atbash_cipher = str.maketrans(string.ascii_lowercase, ''.join(reversed(string.ascii_lowercase)))
_no_spaces = str.maketrans('', '', string.whitespace)
_no_punctuation = str.maketrans('', '', string.punctuation)

def encode(text):
    text = text.translate(_no_spaces).translate(_no_punctuation).lower().translate(atbash_cipher)
    return ' '.join(text[i:i+5] for i in range(0, len(text), 5))

def decode(text):
    return text.translate(_no_spaces).translate(atbash_cipher)

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?