-
-
Notifications
You must be signed in to change notification settings - Fork 354
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
2 changed files
with
136 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using NUnit.Framework; | ||
|
||
[TestFixture] | ||
public class AnagramTest | ||
{ | ||
[Test] | ||
public void NoMatches () | ||
{ | ||
Anagram detector = new Anagram("diaper"); | ||
string[] words = new string[] { "hello", "world", "zombies", "pants" }; | ||
string[] results = new string[0]; | ||
Assert.AreEqual(results, detector.Match(words)); | ||
} | ||
|
||
|
||
[Test] | ||
public void DetectSimpleAnagram () | ||
{ | ||
Anagram detector = new Anagram("ant"); | ||
string[] words = new string[] { "tan", "stand", "at" }; | ||
string[] results = new string[] { "tan" }; | ||
Assert.AreEqual(results, detector.Match(words)); | ||
} | ||
|
||
[Test] | ||
public void DetectMultipleAnagrams () | ||
{ | ||
Anagram detector = new Anagram("master"); | ||
string[] words = new string[] { "stream", "pigeon", "maters" }; | ||
string[] results = new string[] { "maters", "stream" }; | ||
Assert.AreEqual(results, detector.Match(words)); | ||
} | ||
|
||
[Test] | ||
public void DoesNotConfuseDifferentDuplicates () | ||
{ | ||
Anagram detector = new Anagram("galea"); | ||
string[] words = new string[] { "eagle" }; | ||
string[] results = new string[0]; | ||
Assert.AreEqual(results, detector.Match(words)); | ||
} | ||
|
||
[Test] | ||
public void IdenticalWordIsNotAnagram () | ||
{ | ||
Anagram detector = new Anagram("corn"); | ||
string[] words = new string[] { "corn", "dark", "Corn", "rank", "CORN", "cron", "park" }; | ||
string[] results = new string[] { "cron" }; | ||
Assert.AreEqual(results, detector.Match(words)); | ||
} | ||
|
||
[Test] | ||
public void EliminateAnagramsWithSameChecksum () | ||
{ | ||
Anagram detector = new Anagram("mass"); | ||
string[] words = new string[] { "last" }; | ||
string[] results = new string[0]; | ||
Assert.AreEqual(results, detector.Match(words)); | ||
} | ||
|
||
[Test] | ||
public void EliminateAnagramSubsets () | ||
{ | ||
Anagram detector = new Anagram("good"); | ||
string[] words = new string[] { "dog", "goody" }; | ||
string[] results = new string[0]; | ||
Assert.AreEqual(results, detector.Match(words)); | ||
} | ||
|
||
[Test] | ||
public void DetectAnagrams () | ||
{ | ||
Anagram detector = new Anagram("allergy"); | ||
string[] words = new string[] { "gallery", "ballerina", "regally", "clergy", "largely", "leading" }; | ||
string[] results = new string[] { "gallery", "largely", "regally" }; | ||
Assert.AreEqual(results, detector.Match(words)); | ||
} | ||
|
||
[Test] | ||
public void AnagramsAreCaseInsensitive () | ||
{ | ||
Anagram detector = new Anagram("Orchestra"); | ||
string[] words = new string[] { "cashregister", "Carthorse", "radishes" }; | ||
string[] results = new string[] { "Carthorse" }; | ||
Assert.AreEqual(results, detector.Match(words)); | ||
} | ||
|
||
} |
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,48 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Collections.Generic; | ||
|
||
public class Anagram | ||
{ | ||
private string baseWord; | ||
|
||
public Anagram (string baseWord) | ||
{ | ||
this.baseWord = baseWord; | ||
} | ||
|
||
public string[] Match (string[] potentialMatches) | ||
{ | ||
List<string> matches = new List<string>(); | ||
|
||
foreach (string word in potentialMatches) | ||
{ | ||
if (IsWordAnagramOfBaseWord(word)) | ||
{ | ||
matches.Add(word); | ||
} | ||
} | ||
|
||
return matches.OrderBy(word => word).ToArray(); | ||
} | ||
|
||
private bool IsWordAnagramOfBaseWord (string word) | ||
{ | ||
return (IsNotTheSameWordAsBaseWord(word) && HasSameLettersAsBaseWord(word)); | ||
} | ||
|
||
private bool IsNotTheSameWordAsBaseWord (string word) | ||
{ | ||
return ! baseWord.Equals(word,StringComparison.OrdinalIgnoreCase); | ||
} | ||
|
||
private bool HasSameLettersAsBaseWord (string word) | ||
{ | ||
return SortedCharArrayForString(baseWord).Equals(SortedCharArrayForString(word)); | ||
} | ||
|
||
private string SortedCharArrayForString (string word) | ||
{ | ||
return String.Concat(word.ToLower().OrderBy(letter => letter)); | ||
} | ||
} |