Line Up
Write the ticket message a deli gives its customers, with the customer’s place in line as an English ordinal: 1st, 2nd, 3rd, 4th, 11th, 21st.
Adapted from Exercism’s problem specification (MIT). Listed under Strings & Text.
// FILE: line_up.nomi
//# The ticket messages of a deli that serves its customers in line.
/// The ticket message for the customer `name`, served `number`th today.
//! assert format("Mary", 1) == "Mary, you are the 1st customer we serve today. Thank you!"
//
pub fn format(name: String, number: Int) -> String
"${name}, you are the ${number}${suffix(number)} customer we serve today. Thank you!"
end
/// The English ordinal suffix of `number`: 1st, 2nd, 3rd, then th, except
/// that 11, 12 and 13 take th in every hundred.
fn suffix(number: Int) -> String
case (number % 100, number % 10)
(11 or 12 or 13, _) -> "th"
(_, 1) -> "st"
(_, 2) -> "nd"
(_, 3) -> "rd"
_ -> "th"
end
end
// FILE: line_up_test.nomi
import line_up
test "format"
[
("format smallest non-exceptional ordinal numeral 4", "Gianna", 4, "4th"),
("format greatest single digit non-exceptional ordinal numeral 9", "Maarten", 9, "9th"),
("format non-exceptional ordinal numeral 5", "Petronila", 5, "5th"),
("format non-exceptional ordinal numeral 6", "Attakullakulla", 6, "6th"),
("format non-exceptional ordinal numeral 7", "Kate", 7, "7th"),
("format non-exceptional ordinal numeral 8", "Maximiliano", 8, "8th"),
("format exceptional ordinal numeral 1", "Mary", 1, "1st"),
("format exceptional ordinal numeral 2", "Haruto", 2, "2nd"),
("format exceptional ordinal numeral 3", "Henriette", 3, "3rd"),
("format smallest two digit non-exceptional ordinal numeral 10", "Alvarez", 10, "10th"),
("format non-exceptional ordinal numeral 11", "Jacqueline", 11, "11th"),
("format non-exceptional ordinal numeral 12", "Juan", 12, "12th"),
("format non-exceptional ordinal numeral 13", "Patricia", 13, "13th"),
("format exceptional ordinal numeral 21", "Washi", 21, "21st"),
(
"format exceptional ordinal numeral 22 ending in nd even though it is a multiple of 11",
"Ingrid",
22,
"22nd",
),
(
"format exceptional ordinal numeral 33 ending in rd even though it is a multiple of 11",
"Mario",
33,
"33rd",
),
(
"format exceptional ordinal numeral 52 ending in nd even though it is a multiple of 13",
"Quentin",
52,
"52nd",
),
("format exceptional ordinal numeral 62", "Nayra", 62, "62nd"),
(
"format non-exceptional ordinal numeral 72 ending in nd even though it is a multiple of 12",
"Ugo",
72,
"72nd",
),
(
"format exceptional ordinal numeral 91 ending in st even though it is a multiple of 13",
"Boris",
91,
"91st",
),
("format exceptional ordinal numeral 100", "John", 100, "100th"),
("format exceptional ordinal numeral 101", "Zeinab", 101, "101st"),
("format non-exceptional ordinal numeral 112", "Knud", 112, "112th"),
("format exceptional ordinal numeral 123", "Yma", 123, "123rd"),
(
"format large number 972 ending in nd even though it is a multiple of 12",
"Elias",
972,
"972nd",
),
]
|> subtests (description, name, number, ordinal), description
assert line_up.format(name, number)
== "${name}, you are the ${ordinal} customer we serve today. Thank you!"
end
endok format (25 cases)
test result: ok. 25 passed, 0 failed
How it’s written
Section titled “How it’s written”formatis one interpolated string, andsuffixonecaseon the pair(number % 100, number % 10). The first arm catches 11, 12 and 13 in every hundred, which take “th” whatever their last digit; the next three read the last digit, and everything else is “th”. The tuple keeps the exception and the rule in one match instead of anifin front of acase.- std/plural’s
PluralCategory.ordinal(number, Locale"en")answers the same four classes from CLDR’s rules (One,Two,Few,Other), and acaseon it could pick the suffix. The arithmetic is kept: it is the rule the exercise asks for, and it is as short. - The 25 canonical cases are one
subteststable. Each row holds the name, the number and the ordinal the message should carry, and the body builds the expected sentence from them, so the table shows only what differs from row to row.