Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
"topics": [
"Integers"
]
},
{
"slug": "anagram",
"difficulty": 4,
"topics": [
"Strings",
"Filtering"
]
}
],
"deprecated": [
Expand Down
8 changes: 8 additions & 0 deletions exercises/anagram/example/Anagram.ceylon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{String*} anagrams(String subject, {String*} candidates) {
value lower = subject.lowercased;
value chars = sort(lower);
return candidates.filter((c) {
value lowerCandidate = c.lowercased;
return lower != lowerCandidate && chars == sort(lowerCandidate);
});
}
3 changes: 3 additions & 0 deletions exercises/anagram/source/anagram/Anagram.ceylon
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{String*} anagrams(String subject, {String*} candidates) {
return nothing;
}
47 changes: 47 additions & 0 deletions exercises/anagram/source/anagram/AnagramTest.ceylon
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import ceylon.test { ... }

shared {[String, {String*}, {String*}]*} cases =>
{
// no matches
["diaper", {"hello", "world", "zombies", "pants"}, {}],
// detects simple anagram
["ant", {"tan", "stand", "at"}, {"tan"}],
// does not detect false positives
["galea", {"eagle"}, {}],
// detects multiple anagrams
["master", {"stream", "pigeon", "maters"}, {"stream", "maters"}],
// does not detect anagram subsets
["good", {"dog", "goody"}, {}],
// detects anagram
["listen", {"enlists", "google", "inlets", "banana"}, {"inlets"}],
// detects multiple anagrams
[
"allergy",
{"gallery", "ballerina", "regally", "clergy", "largely", "leading"},
{"gallery", "regally", "largely"}
],
// does not detect identical words
["corn", {"corn", "dark", "Corn", "rank", "CORN", "cron", "park"}, {"cron"}],
// 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 a word as its own anagram
["banana", {"Banana"}, {}],
// does not detect a anagram if the original word is repeated
["go", {"go Go GO"}, {}],
// anagrams must use all letters exactly once
["tapper", {"patter"}, {}],
// capital word is not own anagram
["BANANA", {"Banana"}, {}]
};

test
parameters(`value cases`)
shared void testAnagram(String subject, {String*} candidates, {String*} expected) {
assertEquals(sort(anagrams(subject, candidates)), sort(expected));
}
3 changes: 3 additions & 0 deletions exercises/anagram/source/anagram/module.ceylon
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module anagram "1.0" {
import "ceylon.test" "1.3.1";
}