Anagram
Pick out the candidates that are anagrams of a word, ignoring case, where a word is not an anagram of itself.
Adapted from Exercism’s problem specification (MIT). Listed under Strings & Text.
// FILE: anagram.nomi
//# Picks the anagrams of a word out of a list of candidates.
/// The candidates that are anagrams of `subject`, in their original order.
/// Letter case does not matter, and a word is not an anagram of itself.
pub fn find(subject: String, candidates: List<String>) -> List<String>
subject = String.fold_case(subject)
letters = Iter.sort(subject)
candidates
|> Iter.filter(fn candidate ->
candidate = String.fold_case(candidate)
candidate != subject and Iter.sort(candidate) == letters
end)
|> Iter.to_list()
end
// FILE: anagram_test.nomi
import anagram
test "finds anagrams"
[
("no matches", "diaper", ["hello", "world", "zombies", "pants"], []),
("detects two anagrams", "solemn", ["lemons", "cherry", "melons"], ["lemons", "melons"]),
("does not detect anagram subsets", "good", ["dog", "goody"], []),
("detects anagram", "listen", ["enlists", "google", "inlets", "banana"], ["inlets"]),
(
"detects three anagrams",
"allergy",
["gallery", "ballerina", "regally", "clergy", "largely", "leading"],
["gallery", "regally", "largely"],
),
(
"detects multiple anagrams with different case",
"nose",
["Eons", "ONES"],
["Eons", "ONES"],
),
("does not detect non-anagrams with identical checksum", "mass", ["last"], []),
(
"detects anagrams case-insensitively",
"Orchestra",
["cashregister", "Carthorse", "radishes"],
["Carthorse"],
),
(
"detects anagrams using case-insensitive subject",
"Orchestra",
["cashregister", "carthorse", "radishes"],
["carthorse"],
),
(
"detects anagrams using case-insensitive possible matches",
"orchestra",
["cashregister", "Carthorse", "radishes"],
["Carthorse"],
),
("does not detect an anagram if the original word is repeated", "go", ["goGoGO"], []),
("anagrams must use all letters exactly once", "tapper", ["patter"], []),
("words are not anagrams of themselves", "BANANA", ["BANANA"], []),
(
"words are not anagrams of themselves even if letter case is partially different",
"BANANA",
["Banana"],
[],
),
(
"words are not anagrams of themselves even if letter case is completely different",
"BANANA",
["banana"],
[],
),
(
"words other than themselves can be anagrams",
"LISTEN",
["LISTEN", "Silent"],
["Silent"],
),
("handles case of greek letters", "ΑΒΓ", ["ΒΓΑ", "ΒΓΔ", "γβα", "αβγ"], ["ΒΓΑ", "γβα"]),
("different characters may have the same bytes", "a⬂", ["€a"], []),
]
|> subtests (description, subject, candidates, expected), description
assert anagram.find(subject, candidates) == expected
end
endok finds anagrams (18 cases)
test result: ok. 18 passed, 0 failed
How it’s written
Section titled “How it’s written”- The anagram key is
Iter.sort(word): aStringiterates by grapheme, so sorting it gives aList<String>to compare, with no codepoint conversion. - Case is ignored with
String.fold_caseon both sides, once for the subject and once per candidate. Folding is Unicode’s caseless matching, whichString.lowercaseonly approximates: the two Greek sigmasςandσfold to one letter and lower-case to two. - All 18 current cases are one
subteststable, since every row is the same call and comparison. The four cases that newer ones reimplement are left out. - Each row carries its canonical description as its first column and is named by it (
subtests cases, (description, subject, candidates, expected), description), so a failing row reports under its description.