Created
April 26, 2019 09:32
-
-
Save sinannar/0750033d2c71a197be86bcbf98725e53 to your computer and use it in GitHub Desktop.
Example solution for known problem called Anagram of strings
This file contains hidden or 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
using System; | |
using System.Linq; | |
using System.Collections.Generic; | |
public class Program | |
{ | |
public static void Main() | |
{ | |
foreach (var test in GetTestCases()) { | |
var result = IsAnagramPair(test.First, test.Second); | |
Console.WriteLine((result == test.Expected) ? "Success" : "FAIL"); | |
} | |
} | |
public static bool IsAnagramPair(string first, string second) | |
{ | |
return GetOrderedLowerCaseLetters(first) | |
.SequenceEqual(GetOrderedLowerCaseLetters(second)); | |
} | |
public static IOrderedEnumerable<char> GetOrderedLowerCaseLetters(string given) | |
{ | |
return given | |
.ToLower() | |
.ToCharArray() | |
.Where(c => Char.IsLetter((c))) | |
.OrderBy(c => c); | |
} | |
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 }; | |
} | |
} | |
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