Crypto Square
Encode a message by writing its letters and digits in the rows of a near-square rectangle and reading it down the columns.
Adapted from Exercism’s problem specification (MIT). Listed under Encoding & Ciphers.
// FILE: crypto_square.nomi
//# The square code: the message's letters and digits written in rows of a
//# rectangle, then read down the columns.
/// The square code of `plaintext`: its columns, separated by spaces, each as
/// long as the rectangle is tall.
pub fn ciphertext(plaintext: String) -> String
text = normalize(plaintext)
length = String.length(text)
if length == 0 then return ""
columns = width(length)
rows = (length + columns - 1) / columns
text
|> String.pad_end(rows * columns)
|> Iter.chunks(columns)
|> Iter.transpose()
|> Iter.map(String.join)
|> String.join(" ")
end
/// `text` in lower case, with everything but letters and digits removed.
fn normalize(text: String) -> String
text
|> String.lowercase()
|> String.to_codepoints()
|> Iter.filter(fn cp -> Codepoint.is_letter(cp) or Codepoint.is_digit(cp))
|> Iter.to_list()
|> String.from_codepoints()
end
/// The fewest columns that hold `length` letters in a rectangle no taller
/// than it is wide.
fn width(length: PositiveInt) -> PositiveInt
root = Int.sqrt(length)
// A positive length has a root of at least 1.
if root * root == length then root else root + 1
end
// FILE: crypto_square_test.nomi
import crypto_square
test "crypto square"
[
("empty plaintext results in an empty ciphertext", "", ""),
("normalization results in empty plaintext", "... --- ...", ""),
("Lowercase", "A", "a"),
("Remove spaces", " b ", "b"),
("Remove punctuation", "@1,%!", "1"),
(
"9 character plaintext results in 3 chunks of 3 characters",
"This is fun!",
"tsf hiu isn",
),
(
"8 character plaintext results in 3 chunks, the last one with a trailing space",
"Chill out.",
"clu hlt io ",
),
(
"54 character plaintext results in 8 chunks, the last two with trailing spaces",
"If man was meant to stay on the ground, god would have given us roots.",
"imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau ",
),
]
|> subtests (description, plaintext, expected), description
assert crypto_square.ciphertext(plaintext) == expected
end
endok crypto square (8 cases)
test result: ok. 8 passed, 0 failed
How it’s written
Section titled “How it’s written”- The rectangle is never a grid of cells. The normalized text is padded with spaces to fill the rectangle (
String.pad_end), cut into rows withIter.chunks, and turned into columns withIter.transpose; each column is joined into a word and the words are joined with spaces. The padding is where the canonical trailing spaces come from. - The width is the integer square root rounded up,
Int.sqrtplus one when the length is not a square, and the height is the length divided by the width, rounded up. That pair is the smallest rectangle with the width at most one more than the height. - Normalizing works on codepoints:
String.lowercase, then keep eachCodepoint.is_letterorCodepoint.is_digit. - Empty text returns
""before the width is worked out, so nothing divides by a width of zero. - The canonical cases are one
subteststable. The canonical data replaces its “54 character plaintext results in 7 chunks” case with one that says 8 chunks and expects the same text (reimplements); the table holds only the replacement. - The empty text returns early on
length == 0. The length is aNonNegativeInt, so below the check it is aPositiveInt, andwidthtakes aPositiveIntandInt.sqrttakes it as it is.