Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9331ce0
commit af30cf7
Showing
5 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{String*} anagrams(String subject, {String*} candidates) { | ||
return nothing; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module anagram "1.0" { | ||
import "ceylon.test" "1.3.1"; | ||
} |