-
-
Notifications
You must be signed in to change notification settings - Fork 32
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
Showing
8 changed files
with
352 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,13 @@ | ||
# Instructions | ||
|
||
Your task is to, given a target word and a set of candidate words, to find the subset of the candidates that are anagrams of the target. | ||
|
||
An anagram is a rearrangement of letters to form a new word: for example `"owns"` is an anagram of `"snow"`. | ||
A word is _not_ its own anagram: for example, `"stop"` is not an anagram of `"stop"`. | ||
|
||
The target and candidates are words of one or more ASCII alphabetic characters (`A`-`Z` and `a`-`z`). | ||
Lowercase and uppercase characters are equivalent: for example, `"PoTS"` is an anagram of `"sTOp"`, but `StoP` is not an anagram of `sTOp`. | ||
The anagram set is the subset of the candidate set that are anagrams of the target (in any order). | ||
Words in the anagram set should have the same letter case as in the candidate set. | ||
|
||
Given the target `"stone"` and candidates `"stone"`, `"tones"`, `"banana"`, `"tons"`, `"notes"`, `"Seton"`, the anagram set is `"tones"`, `"notes"`, `"Seton"`. |
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,12 @@ | ||
# Introduction | ||
|
||
At a garage sale, you find a lovely vintage typewriter at a bargain price! | ||
Excitedly, you rush home, insert a sheet of paper, and start typing away. | ||
However, your excitement wanes when you examine the output: all words are garbled! | ||
For example, it prints "stop" instead of "post" and "least" instead of "stale." | ||
Carefully, you try again, but now it prints "spot" and "slate." | ||
After some experimentation, you find there is a random delay before each letter is printed, which messes up the order. | ||
You now understand why they sold it for so little money! | ||
|
||
You realize this quirk allows you to generate anagrams, which are words formed by rearranging the letters of another word. | ||
Pleased with your finding, you spend the rest of the day generating hundreds of anagrams. |
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,19 @@ | ||
{ | ||
"authors": [ | ||
"BNAndras" | ||
], | ||
"files": { | ||
"solution": [ | ||
"source/anagram.d" | ||
], | ||
"test": [ | ||
"source/anagram.d" | ||
], | ||
"example": [ | ||
"example/anagram.d" | ||
] | ||
}, | ||
"blurb": "Given a word and a list of possible anagrams, select the correct sublist.", | ||
"source": "Inspired by the Extreme Startup game", | ||
"source_url": "https://github.com/rchatley/extreme_startup" | ||
} |
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,88 @@ | ||
# This is an auto-generated file. | ||
# | ||
# Regenerating this file via `configlet sync` will: | ||
# - Recreate every `description` key/value pair | ||
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
# - Preserve any other key/value pair | ||
# | ||
# As user-added comments (using the # character) will be removed when this file | ||
# is regenerated, comments can be added via a `comment` key. | ||
|
||
[dd40c4d2-3c8b-44e5-992a-f42b393ec373] | ||
description = "no matches" | ||
|
||
[b3cca662-f50a-489e-ae10-ab8290a09bdc] | ||
description = "detects two anagrams" | ||
include = false | ||
|
||
[03eb9bbe-8906-4ea0-84fa-ffe711b52c8b] | ||
description = "detects two anagrams" | ||
reimplements = "b3cca662-f50a-489e-ae10-ab8290a09bdc" | ||
|
||
[a27558ee-9ba0-4552-96b1-ecf665b06556] | ||
description = "does not detect anagram subsets" | ||
|
||
[64cd4584-fc15-4781-b633-3d814c4941a4] | ||
description = "detects anagram" | ||
|
||
[99c91beb-838f-4ccd-b123-935139917283] | ||
description = "detects three anagrams" | ||
|
||
[78487770-e258-4e1f-a646-8ece10950d90] | ||
description = "detects multiple anagrams with different case" | ||
|
||
[1d0ab8aa-362f-49b7-9902-3d0c668d557b] | ||
description = "does not detect non-anagrams with identical checksum" | ||
|
||
[9e632c0b-c0b1-4804-8cc1-e295dea6d8a8] | ||
description = "detects anagrams case-insensitively" | ||
|
||
[b248e49f-0905-48d2-9c8d-bd02d8c3e392] | ||
description = "detects anagrams using case-insensitive subject" | ||
|
||
[f367325c-78ec-411c-be76-e79047f4bd54] | ||
description = "detects anagrams using case-insensitive possible matches" | ||
|
||
[7cc195ad-e3c7-44ee-9fd2-d3c344806a2c] | ||
description = "does not detect an anagram if the original word is repeated" | ||
include = false | ||
|
||
[630abb71-a94e-4715-8395-179ec1df9f91] | ||
description = "does not detect an anagram if the original word is repeated" | ||
reimplements = "7cc195ad-e3c7-44ee-9fd2-d3c344806a2c" | ||
|
||
[9878a1c9-d6ea-4235-ae51-3ea2befd6842] | ||
description = "anagrams must use all letters exactly once" | ||
|
||
[85757361-4535-45fd-ac0e-3810d40debc1] | ||
description = "words are not anagrams of themselves (case-insensitive)" | ||
include = false | ||
|
||
[68934ed0-010b-4ef9-857a-20c9012d1ebf] | ||
description = "words are not anagrams of themselves" | ||
reimplements = "85757361-4535-45fd-ac0e-3810d40debc1" | ||
|
||
[589384f3-4c8a-4e7d-9edc-51c3e5f0c90e] | ||
description = "words are not anagrams of themselves even if letter case is partially different" | ||
reimplements = "85757361-4535-45fd-ac0e-3810d40debc1" | ||
|
||
[ba53e423-7e02-41ee-9ae2-71f91e6d18e6] | ||
description = "words are not anagrams of themselves even if letter case is completely different" | ||
reimplements = "85757361-4535-45fd-ac0e-3810d40debc1" | ||
|
||
[a0705568-628c-4b55-9798-82e4acde51ca] | ||
description = "words other than themselves can be anagrams" | ||
include = false | ||
|
||
[33d3f67e-fbb9-49d3-a90e-0beb00861da7] | ||
description = "words other than themselves can be anagrams" | ||
reimplements = "a0705568-628c-4b55-9798-82e4acde51ca" | ||
|
||
[a6854f66-eec1-4afd-a137-62ef2870c051] | ||
description = "handles case of greek letters" | ||
|
||
[fd3509e5-e3ba-409d-ac3d-a9ac84d13296] | ||
description = "different characters may have the same bytes" | ||
include = false | ||
|
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,2 @@ | ||
name "anagram" | ||
buildRequirements "disallowDeprecations" |
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,24 @@ | ||
import std.array; | ||
import std.string; | ||
import std.algorithm; | ||
|
||
pure string[] findAnagrams(immutable string subject, immutable string[] candidates) | ||
{ | ||
string[] results = []; | ||
|
||
string loweredSubject = subject.toLower(); | ||
auto subjectLetters = loweredSubject.split("").sort(); | ||
|
||
foreach (candidate; candidates) | ||
{ | ||
string loweredCandidate = candidate.toLower(); | ||
auto candidateLetters = loweredCandidate.split("").sort(); | ||
|
||
if (subjectLetters == candidateLetters && loweredSubject != loweredCandidate) | ||
{ | ||
results ~= candidate; | ||
} | ||
} | ||
return results; | ||
} | ||
|
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,186 @@ | ||
module anagram; | ||
|
||
pure string[] findAnagrams(immutable string subject, immutable string[] candidates) | ||
{ | ||
// implement this function | ||
} | ||
|
||
unittest | ||
{ | ||
immutable int allTestsEnabled = 0; | ||
|
||
// no matches | ||
{ | ||
immutable string subject = "diaper"; | ||
immutable string[] candidates = ["hello", "world", "zombies", "pants"]; | ||
string[] actual = findAnagrams(subject, candidates); | ||
string[] expected = []; | ||
|
||
assert(actual == expected); | ||
} | ||
|
||
static if (allTestsEnabled) | ||
{ | ||
|
||
// detects two anagrams | ||
{ | ||
immutable string subject = "solemn"; | ||
immutable string[] candidates = ["lemons", "cherry", "melons"]; | ||
string[] actual = findAnagrams(subject, candidates); | ||
string[] expected = ["lemons", "melons"]; | ||
|
||
assert(actual == expected); | ||
} | ||
|
||
// does not detect anagram subsets | ||
{ | ||
immutable string subject = "good"; | ||
immutable string[] candidates = ["dog", "goody"]; | ||
string[] actual = findAnagrams(subject, candidates); | ||
string[] expected = []; | ||
|
||
assert(actual == expected); | ||
} | ||
|
||
// detects anagram | ||
{ | ||
immutable string subject = "listen"; | ||
immutable string[] candidates = ["enlists", "google", "inlets", "banana"]; | ||
string[] actual = findAnagrams(subject, candidates); | ||
string[] expected = ["inlets"]; | ||
|
||
assert(actual == expected); | ||
} | ||
|
||
// detects three anagrams | ||
{ | ||
immutable string subject = "allergy"; | ||
immutable string[] candidates = ["gallery", "ballerina", "regally", "clergy", "largely", "leading"]; | ||
string[] actual = findAnagrams(subject, candidates); | ||
string[] expected = ["gallery", "regally", "largely"]; | ||
|
||
assert(actual == expected); | ||
} | ||
|
||
// detects multiple anagrams with different case | ||
{ | ||
immutable string subject = "nose"; | ||
immutable string[] candidates = ["Eons", "ONES"]; | ||
string[] actual = findAnagrams(subject, candidates); | ||
string[] expected = ["Eons", "ONES"]; | ||
|
||
assert(actual == expected); | ||
} | ||
|
||
// does not detect non-anagrams with identical checksum | ||
{ | ||
immutable string subject = "mass"; | ||
immutable string[] candidates = ["last"]; | ||
string[] actual = findAnagrams(subject, candidates); | ||
string[] expected = []; | ||
|
||
assert(actual == expected); | ||
} | ||
|
||
// detects anagrams case-insensitively | ||
{ | ||
immutable string subject = "Orchestra"; | ||
immutable string[] candidates = ["cashregister", "Carthorse", "radishes"]; | ||
string[] actual = findAnagrams(subject, candidates); | ||
string[] expected = ["Carthorse"]; | ||
|
||
assert(actual == expected); | ||
} | ||
|
||
// detects anagrams using case-insensitive subject | ||
{ | ||
immutable string subject = "Orchestra"; | ||
immutable string[] candidates = ["cashregister", "carthorse", "radishes"]; | ||
string[] actual = findAnagrams(subject, candidates); | ||
string[] expected = ["carthorse"]; | ||
|
||
assert(actual == expected); | ||
} | ||
|
||
// detects anagrams using case-insensitive possible matches | ||
{ | ||
immutable string subject = "orchestra"; | ||
immutable string[] candidates = ["cashregister", "Carthorse", "radishes"]; | ||
string[] actual = findAnagrams(subject, candidates); | ||
string[] expected = ["Carthorse"]; | ||
|
||
assert(actual == expected); | ||
} | ||
|
||
// does not detect an anagram if the original word is repeated | ||
{ | ||
immutable string subject = "go"; | ||
immutable string[] candidates = ["goGoGO"]; | ||
string[] actual = findAnagrams(subject, candidates); | ||
string[] expected = []; | ||
|
||
assert(actual == expected); | ||
} | ||
|
||
// anagrams must use all letters exactly once | ||
{ | ||
immutable string subject = "tapper"; | ||
immutable string[] candidates = ["patter"]; | ||
string[] actual = findAnagrams(subject, candidates); | ||
string[] expected = []; | ||
|
||
assert(actual == expected); | ||
} | ||
|
||
// words are not anagrams of themselves | ||
{ | ||
immutable string subject = "BANANA"; | ||
immutable string[] candidates = ["BANANA"]; | ||
string[] actual = findAnagrams(subject, candidates); | ||
string[] expected = []; | ||
|
||
assert(actual == expected); | ||
} | ||
|
||
// words are not anagrams of themselves even if letter case is partially different | ||
{ | ||
immutable string subject = "BANANA"; | ||
immutable string[] candidates = ["Banana"]; | ||
string[] actual = findAnagrams(subject, candidates); | ||
string[] expected = []; | ||
|
||
assert(actual == expected); | ||
} | ||
|
||
// words are not anagrams of themselves even if letter case is completely different | ||
{ | ||
immutable string subject = "BANANA"; | ||
immutable string[] candidates = ["banana"]; | ||
string[] actual = findAnagrams(subject, candidates); | ||
string[] expected = []; | ||
|
||
assert(actual == expected); | ||
} | ||
|
||
// words other than themselves can be anagrams | ||
{ | ||
immutable string subject = "LISTEN"; | ||
immutable string[] candidates = ["Listen", "Silent", "LISTEN"]; | ||
string[] actual = findAnagrams(subject, candidates); | ||
string[] expected = ["Silent"]; | ||
|
||
assert(actual == expected); | ||
} | ||
|
||
// handles case of greek letters | ||
{ | ||
immutable string subject = "ΑΒΓ"; | ||
immutable string[] candidates = ["ΒΓΑ", "ΒΓΔ", "γβα", "αβγ"]; | ||
string[] actual = findAnagrams(subject, candidates); | ||
string[] expected = ["ΒΓΑ", "γβα"]; | ||
|
||
assert(actual == expected); | ||
} | ||
} | ||
} | ||
|