Atbash Cipher
Encode and decode text by swapping each letter with its mirror in the alphabet.
Adapted from Exercism’s problem specification (MIT). Listed under Encoding & Ciphers.
// FILE: atbash_cipher.nomi
//# The Atbash cipher: each letter swaps with its mirror in the alphabet, `a`
//# with `z`, `b` with `y`, and so on. Digits pass through unchanged, and
//# everything else is dropped.
/// Each lowercase letter and its mirror.
once mirror = Iter.zip('a'..='z', Iter.reverse('a'..='z')) |> Iter.to_map()
/// The ciphertext of `phrase`, in lowercase groups of five.
pub fn encode(phrase: String) -> String
phrase
|> translate()
|> Iter.chunks(5)
|> Iter.map(String.join)
|> String.join(" ")
end
/// The plaintext of `phrase`, with no spaces.
pub fn decode(phrase: String) -> String
phrase |> translate() |> String.join()
end
/// Each letter and digit of `text` in cipher order. The cipher is its own
/// inverse, so this both encodes and decodes.
fn translate(text: String) -> List<String>
text
|> String.lowercase()
|> String.to_codepoints()
|> Iter.flat_map(swap)
|> Iter.map(Codepoint.to_string)
|> Iter.to_list()
end
fn swap(cp: Codepoint) -> Maybe<Codepoint>
if Codepoint.is_digit(cp) then
Some(cp)
else
Map.get(mirror, cp)
end
end
// FILE: atbash_cipher_test.nomi
import atbash_cipher
test "encode"
[
("encode yes", "yes", "bvh"),
("encode no", "no", "ml"),
("encode OMG", "OMG", "lnt"),
("encode spaces", "O M G", "lnt"),
("encode mindblowingly", "mindblowingly", "nrmwy oldrm tob"),
("encode numbers", "Testing,1 2 3, testing.", "gvhgr mt123 gvhgr mt"),
("encode deep thought", "Truth is fiction.", "gifgs rhurx grlm"),
(
"encode all the letters",
"The quick brown fox jumps over the lazy dog.",
"gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt",
),
]
|> subtests (description, phrase, expected), description
assert atbash_cipher.encode(phrase) == expected
end
end
test "decode"
[
("decode exercism", "vcvix rhn", "exercism"),
(
"decode a sentence",
"zmlyh gzxov rhlug vmzhg vkkrm thglm v",
"anobstacleisoftenasteppingstone",
),
("decode numbers", "gvhgr mt123 gvhgr mt", "testing123testing"),
(
"decode all the letters",
"gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt",
"thequickbrownfoxjumpsoverthelazydog",
),
("decode with too many spaces", "vc vix r hn", "exercism"),
(
"decode with no spaces",
"zmlyhgzxovrhlugvmzhgvkkrmthglmv",
"anobstacleisoftenasteppingstone",
),
]
|> subtests (description, phrase, expected), description
assert atbash_cipher.decode(phrase) == expected
end
endok encode (8 cases)
ok decode (6 cases)
test result: ok. 14 passed, 0 failed
How it’s written
Section titled “How it’s written”- The cipher is a map from each letter to its mirror,
Iter.zip('a'..='z', Iter.reverse('a'..='z')) |> Iter.to_map(), built once. Arithmetic on code point numbers (219 - n) was the other option; the map shows the two alphabets the description shows. translateworks on code points, since the cipher is ASCII.swapkeeps a digit and looks a letter up in the map, so anything else returnsNone, andIter.flat_map(swap)drops it: aMaybeiterates as zero or one value.- The cipher is its own inverse, so
encodeanddecodesharetranslateand differ only in how they join the letters: groups of five fromIter.chunks(5), or one run. - Each canonical group is a test named by the group’s description, holding one
subteststable; agroupblock is for sharedclock,bootorsetup, which none needs.