Skip to content

Instantly share code, notes, and snippets.

@twoteesbrett
Created December 10, 2019 21:10
Show Gist options
  • Save twoteesbrett/daff47c47e14f3961b761a2465ee89e6 to your computer and use it in GitHub Desktop.
Save twoteesbrett/daff47c47e14f3961b761a2465ee89e6 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()
{
Check("abcba", true);
Check("abcde", false);
Check("Mr owl ate my metal worm", true);
Check("Never Odd Or Even", true);
Check("Never Even Or Odd", false);
Check("Do geese see God?", true);
Check("A man, a plan, a canal, Panama!", true);
Check("Ītati", true);
Check("12321", true);
Check("12345", false);
}
private static void Check(string s, bool shouldBePalindrome)
{
Console.WriteLine(s.IsPalindrome() == shouldBePalindrome ? "pass" : "FAIL");
}
}
public static class StringExtensions
{
public static bool IsPalindrome(this string s)
{
var normalised = s.Normalise();
return normalised.SequenceEqual(normalised.Reverse());
}
public static IEnumerable<char> Normalise(this string value)
{
return value
.ToLower()
.Normalize(System.Text.NormalizationForm.FormKD)
.Where(c => char.IsLetter(c) || char.IsDigit(c));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment