Isogram
Decide whether a word or phrase repeats no letter, ignoring case; spaces and hyphens may repeat.
Adapted from Exercism’s problem specification (MIT). Listed under Strings & Text.
// FILE: isogram.nomi
//# Isograms: words and phrases with no repeated letter.
/// Whether no letter appears in `phrase` more than once, ignoring case.
/// Spaces, hyphens and other non-letters may repeat.
pub fn is_isogram(phrase: String) -> Bool
letters =
phrase
|> String.fold_case()
|> String.to_codepoints()
|> Iter.filter(Codepoint.is_letter)
|> Iter.to_list()
letters |> Iter.to_set() |> Set.size() == Iter.count(letters)
end
// FILE: isogram_test.nomi
import isogram.is_isogram
test "isograms"
[
("empty string", "", True),
("isogram with only lower case characters", "isogram", True),
("word with one duplicated character", "eleven", False),
("word with one duplicated character from the end of the alphabet", "zzyzx", False),
("longest reported english isogram", "subdermatoglyphic", True),
("word with duplicated character in mixed case", "Alphabet", False),
("word with duplicated character in mixed case, lowercase first", "alphAbet", False),
("hypothetical isogrammic word with hyphen", "thumbscrew-japingly", True),
(
"hypothetical word with duplicated character following hyphen",
"thumbscrew-jappingly",
False,
),
("isogram with duplicated hyphen", "six-year-old", True),
("made-up name that is an isogram", "Emily Jung Schwartzkopf", True),
("duplicated character in the middle", "accentor", False),
("same first and last characters", "angola", False),
("word with duplicated character and with two hyphens", "up-to-date", False),
]
|> subtests (description, phrase, expected), description
assert is_isogram(phrase) == expected
end
endok isograms (14 cases)
test result: ok. 14 passed, 0 failed
How it’s written
Section titled “How it’s written”- The letters are the case-folded phrase’s codepoints (
String.fold_case, which is for comparing, rather thanString.lowercase) filtered byCodepoint.is_letter, which keeps every Unicode letter and drops everything else.String.find_all(Regex\pL)keeps the same codepoints in one stage instead of three, but a reader has to know\pL, and the filter says “keep the letters” in words. Filtering out" "and"-"by hand matches the problem text but lets any other punctuation count as a letter. - Repeats are detected by comparing the letter count with the size of the letter set, rather than by folding through a set and stopping at the first repeat. The inputs are words, so stopping early buys nothing.