Skip to the content

The mirror cipher

Problem

The Atbash cipher is one of the oldest there is, and it fits in one sentence: every letter is swapped for the one in its place counting from the end of the alphabet. a is the first one, so it becomes z, the last one; b becomes y; c becomes x. It is a mirror set in the middle: m and n trade places, and running a letter through the mirror twice gives you the same letter back.

Write a function that takes a text and returns its enciphered version. The rules: the alphabet is the 26 letters from a to z and the result comes out in lowercase, so an uppercase letter is enciphered as its lowercase one and "OMG" gives "lnt". Digits stay as they are. Everything else disappears from the result: spaces, commas, periods and accented letters, because they are none of those 26 letters (an é does not count as an e: it goes away). With an empty text you return an empty text.

There are two roads and both work: look up which position the letter is in and take the one at the opposite position, or keep the alphabet written backwards on the side and pull the letter from the same spot.

Examples

  • Three letters

    "yes" → "bvh"

  • n and o

    "no" → "ml"

  • All uppercase

    "OMG" → "lnt"

  • Spaces go away

    "the sun" → "gsvhfm"

  • Digits and signs

    "Testing,1 2 3, testing." → "gvhgrmt123gvhgrmt"

  • A phrase

    "Secret message" → "hvxivgnvhhztv"

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def mirror_cipher(text):
    pass

JavaScript

function mirrorCipher(text) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n) challenges

See all challenges →