Skip to content

Commit

Permalink
Add anagram in C#
Browse files Browse the repository at this point in the history
  • Loading branch information
Franklin Webber authored and kytrinyx committed Mar 9, 2014
1 parent 0de867e commit 4d73a89
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
88 changes: 88 additions & 0 deletions anagram/AnagramTest.cs
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));
}

}
48 changes: 48 additions & 0 deletions anagram/Example.cs
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));
}
}

0 comments on commit 4d73a89

Please sign in to comment.