Skip to content

Commit

Permalink
anagram: Add new exercise (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
petertseng committed Jan 14, 2017
1 parent 9331ce0 commit af30cf7
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
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
@@ -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
@@ -0,0 +1,3 @@
{String*} anagrams(String subject, {String*} candidates) {
return nothing;
}
47 changes: 47 additions & 0 deletions exercises/anagram/source/anagram/AnagramTest.ceylon
@@ -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
@@ -0,0 +1,3 @@
module anagram "1.0" {
import "ceylon.test" "1.3.1";
}

0 comments on commit af30cf7

Please sign in to comment.