Created
July 12, 2018 01:11
-
-
Save justin-vanwinkle/5d7447acf6b70073fff272e29e3ce8c5 to your computer and use it in GitHub Desktop.
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
using System.Collections.Generic; | |
using System.Linq; | |
namespace PowerDMS.Main | |
{ | |
public class Interview | |
{ | |
/// <summary> | |
/// Compares two strings for an anagram | |
/// </summary> | |
/// <param name="str1">First string for comparison</param> | |
/// <param name="str2">Second string for comparison</param> | |
/// <returns>True if the two strings form an anagram, otherwise false</returns> | |
public bool IsAnagram(string str1, string str2) | |
{ | |
// **Assumption** Leading/trailing whitespace is acceptable input, but not considered for comparison. | |
str1 = str1.Trim(); | |
str2 = str2.Trim(); | |
// internal whitespace breaks the definition of an anagram | |
if (str1.Any(char.IsWhiteSpace) || str2.Any(char.IsWhiteSpace)) | |
{ | |
return false; | |
} | |
// null strings break the definition of an anagram | |
if (string.IsNullOrWhiteSpace(str1) || string.IsNullOrWhiteSpace(str2)) | |
{ | |
return false; | |
} | |
var str1CharCounts = TallyChars(str1); | |
var str2CharCounts = TallyChars(str2); | |
// check for differring keys and key counts | |
if (str1CharCounts.Count != str2CharCounts.Count || | |
!str1CharCounts.Keys.All(x => str2CharCounts.ContainsKey(x))) | |
{ | |
return false; | |
} | |
foreach (var (key, value) in str1CharCounts) | |
{ | |
if (str2CharCounts[key] != value) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
/// <summary> | |
/// Counts the occurrence of each letter in a given string | |
/// </summary> | |
/// <param name="str">The string containing characters to tally</param> | |
/// <returns>A Dictionary mapping the letters that occurred in the string to the total number of times that they occurred in the string</returns> | |
public Dictionary<char, int> TallyChars(string str) | |
{ | |
var chars = str.ToCharArray(0, str.Length); | |
var map = new Dictionary<char, int>(); | |
foreach (var c in chars) | |
{ | |
if (map.TryGetValue(c, out var currentCount)) | |
{ | |
map[c] = currentCount + 1; | |
} | |
else | |
{ | |
map.Add(c, 1); | |
} | |
} | |
return map; | |
} | |
} | |
} |
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
using FluentAssertions; | |
using PowerDMS.Main; | |
using Xunit; | |
namespace PowerDMS.Tests | |
{ | |
public class InterviewTests | |
{ | |
private Interview Interview { get; } | |
public InterviewTests() | |
{ | |
Interview = new Interview(); | |
} | |
[Fact] | |
public void Returns_True_If_Anagram() | |
{ | |
var str1 = "cinema"; | |
var str2 = "iceman"; | |
Interview.IsAnagram(str1, str2).Should().BeTrue(); | |
} | |
[Fact] | |
public void Returns_False_If_Two_Strings_Have_Same_Length_Different_Letters() | |
{ | |
var str1 = "ars"; | |
var str2 = "set"; | |
Interview.IsAnagram(str1, str2).Should().BeFalse(); | |
} | |
[Fact] | |
public void Returns_False_If_Two_Strings_Have_Differring_Lengths() | |
{ | |
var str1 = "arst"; | |
var str2 = "qw"; | |
Interview.IsAnagram(str1, str2).Should().BeFalse(); | |
} | |
[Fact] | |
public void Returns_False_If_Strings_Have_Same_Letters_Different_Count() | |
{ | |
var str1 = "aarr"; | |
var str2 = "arrr"; | |
Interview.IsAnagram(str1, str2).Should().BeFalse(); | |
} | |
[Fact] | |
public void Returns_False_If_Strings_Contain_Whitespace() | |
{ | |
var str1 = "aa rr"; | |
var str2 = "aa rr"; | |
Interview.IsAnagram(str1, str2).Should().BeFalse(); | |
} | |
[Fact] | |
public void Returns_False_If_String_Is_Whitespace() | |
{ | |
var str1 = ""; | |
var str2 = "aarr"; | |
Interview.IsAnagram(str1, str2).Should().BeFalse(); | |
} | |
[Fact] | |
public void Leading_And_Trailing_Whitespace_Are_Acceptable() | |
{ | |
var str1 = " arst"; | |
var str2 = "sart "; | |
Interview.IsAnagram(str1, str2).Should().BeTrue(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment