Skip to content

Instantly share code, notes, and snippets.

@twoteesbrett
Last active December 10, 2019 21:11
Show Gist options
  • Save twoteesbrett/b16ecdedf68a845fd138ecee5dd5e0be to your computer and use it in GitHub Desktop.
Save twoteesbrett/b16ecdedf68a845fd138ecee5dd5e0be to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
foreach (var test in GetTestCases())
{
var result = test.First.IsAnagram(test.Second);
Console.WriteLine((result == test.Expected) ? "Success" : "FAIL");
}
}
public static IEnumerable<TestCase> GetTestCases()
{
yield return new TestCase { First = "add", Second = "dad", Expected = true };
yield return new TestCase { First = "aad", Second = "dad", Expected = false };
yield return new TestCase { First = "Astronomer", Second = "Moon starer", Expected = true };
yield return new TestCase { First = "thorough", Second = "through", Expected = false };
yield return new TestCase { First = "Jim Morrison", Second = "Mr. Mojo Risin'", Expected = true };
yield return new TestCase { First = "123 Rook Street", Second = "999 Koro street", Expected = true };
}
}
public static class StringExtensions
{
public static bool IsAnagram(this string first, string second)
{
return first.Normalise().SequenceEqual(second.Normalise());
}
public static IEnumerable<char> Normalise(this string value)
{
if (value.Any(c => char.IsDigit(c)))
{
throw new ArgumentException("Digits are not permitted.", "value");
}
return value
.ToLower()
.Where(c => char.IsLetter(c))
.OrderBy(c => c);
}
}
public class TestCase
{
public string First { get; set; }
public string Second { get; set; }
public bool Expected { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment