Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

anagram: Add new exercise #9

Merged
merged 1 commit into from Jan 14, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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";
}