Diamond
Draw a diamond of letters from A out to a given letter and back.
Adapted from Exercism’s problem specification (MIT). Listed under Strings & Text.
// FILE: diamond.nomi
//# Draws a diamond of letters from `A` to a given letter and back.
/// The rows of a diamond of letters from `A` to `widest` and back, each
/// padded with spaces to the diamond's width. `widest` is an uppercase
/// ASCII letter.
pub fn rows(widest: Codepoint) -> List<String>
letters = Iter.to_list('A'..=widest)
size = Iter.count(letters)
top =
letters
|> Iter.with_index()
|> Iter.map(fn (i, letter) -> row(letter, i, size))
|> Iter.to_list()
bottom = top |> Iter.reverse() |> Iter.drop(1)
[..top, ..bottom]
end
/// Row `i` of a diamond `size` letters tall to its middle, holding `letter`.
fn row(letter: Codepoint, i: Int, size: Int) -> String
// `i` counts from 0 to `size - 1`, so neither count is negative.
outside = String.repeat(" ", NonNegativeInt.try_from(size - 1 - i)!)
if i == 0 then
"${outside}${letter}${outside}"
else
inside = String.repeat(" ", NonNegativeInt.try_from(2 * i - 1)!)
"${outside}${letter}${inside}${letter}${outside}"
end
end
// FILE: diamond_test.nomi
import diamond
test "Degenerate case with a single 'A' row"
assert diamond.rows('A') == [
"A",
]
end
test "Degenerate case with no row containing 3 distinct groups of spaces"
assert diamond.rows('B') == [
" A ",
"B B",
" A ",
]
end
test "Smallest non-degenerate case with odd diamond side length"
assert diamond.rows('C') == [
" A ",
" B B ",
"C C",
" B B ",
" A ",
]
end
test "Smallest non-degenerate case with even diamond side length"
assert diamond.rows('D') == [
" A ",
" B B ",
" C C ",
"D D",
" C C ",
" B B ",
" A ",
]
end
test "Largest possible diamond"
assert diamond.rows('Z') == [
" A ",
" B B ",
" C C ",
" D D ",
" E E ",
" F F ",
" G G ",
" H H ",
" I I ",
" J J ",
" K K ",
" L L ",
" M M ",
" N N ",
" O O ",
" P P ",
" Q Q ",
" R R ",
" S S ",
" T T ",
" U U ",
" V V ",
" W W ",
" X X ",
" Y Y ",
"Z Z",
" Y Y ",
" X X ",
" W W ",
" V V ",
" U U ",
" T T ",
" S S ",
" R R ",
" Q Q ",
" P P ",
" O O ",
" N N ",
" M M ",
" L L ",
" K K ",
" J J ",
" I I ",
" H H ",
" G G ",
" F F ",
" E E ",
" D D ",
" C C ",
" B B ",
" A ",
]
endok Degenerate case with a single 'A' row
ok Degenerate case with no row containing 3 distinct groups of spaces
ok Smallest non-degenerate case with odd diamond side length
ok Smallest non-degenerate case with even diamond side length
ok Largest possible diamond
test result: ok. 5 passed, 0 failed
How it’s written
Section titled “How it’s written”rowstakes the widest letter as aCodepoint, so'A'..=widestis the list of letters with no conversion. The canonical input is a one-letter string;'C'says the same and cannot be two letters.- The top half is built row by row from
Iter.with_index, and the bottom half is the top reversed without its last row, joined as[..top, ..bottom]. rowpads withString.repeat. The first row is its own branch, since it holds one letter rather than two.- The five cases are plain
testblocks rather than asubteststable. Each expected value is a list of up to 51 rows, which reads better under its own test name than as a table row.