Created
May 9, 2021 22:40
-
-
Save HELLoWorlD01100/7f1cf77f7c154447a2e30b9c60ef7750 to your computer and use it in GitHub Desktop.
Telephone Coder
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; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace ConsoleApp2 | |
{ | |
public static class Solution | |
{ | |
private static readonly Dictionary<int, IEnumerable<string>> NumbersToLetters = new() | |
{ | |
{2, new[] {"a", "b", "c"}}, | |
{3, new[] {"d", "e", "f"}}, | |
{4, new[] {"g", "h", "i"}}, | |
{5, new[] {"j", "k", "l"}}, | |
{6, new[] {"m", "n", "o"}}, | |
{7, new[] {"p", "q", "r", "s"}}, | |
{8, new[] {"t", "u", "v"}}, | |
{9, new[] {"w", "x", "y", "z"}} | |
}; | |
public static IEnumerable<string> GetPasswordCombination(string input) | |
{ | |
var letters = GetNumbersFromNumberAsString(input).Select(x => NumbersToLetters[x]); | |
return letters.Aggregate((x, y) => x.CombineElements(y)); | |
} | |
private static IEnumerable<int> GetNumbersFromNumberAsString(string input) | |
{ | |
if (!int.TryParse(input.Reverse(), out var number)) | |
throw new InvalidOperationException(); | |
while (number > 0) | |
{ | |
yield return number % 10; | |
number /= 10; | |
} | |
} | |
} | |
public static class EnumerableExtensions | |
{ | |
public static IEnumerable<string> CombineElements(this IEnumerable<string> source, | |
IEnumerable<string> forCombine) | |
{ | |
return from i in source from j in forCombine select i + j; | |
} | |
} | |
public static class StringExtensions | |
{ | |
public static string Reverse(this string input) | |
{ | |
var arr = input.ToCharArray(); | |
Array.Reverse(arr); | |
return new string(arr); | |
} | |
} | |
} |
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 ConsoleApp2; | |
using FluentAssertions; | |
using NUnit.Framework; | |
namespace Tests | |
{ | |
public class Tests | |
{ | |
[TestCase("2", new[] {"a", "b", "c"})] | |
[TestCase("23", new[] {"ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"})] | |
[TestCase("234", | |
new[] | |
{ | |
"adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", "bdg", "bdh", "bdi", "beg", "beh", "bei", | |
"bfg", "bfh", "bfi", "cdg", "cdh", "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi" | |
})] | |
public void GetPasswordCombination_ShouldPass(string input, IEnumerable<string> expected) | |
{ | |
var result = Solution.GetPasswordCombination(input); | |
result.Should().BeEquivalentTo(expected); | |
} | |
[TestCase("123", "321")] | |
[TestCase("abra", "arba")] | |
public void Reverse_ShouldPass(string input, string expected) | |
{ | |
var result = input.Reverse(); | |
result.Should().BeEquivalentTo(expected); | |
} | |
[TestCase(new[] {"a", "b", "c"}, new[] {"a", "b", "c"}, | |
new[] {"aa", "ab", "ac", "ba", "bb", "bc", "ca", "cb", "cc"})] | |
public void CombineElements_ShouldPass(IEnumerable<string> first, IEnumerable<string> second, | |
IEnumerable<string> expected) | |
{ | |
var result = first.CombineElements(second); | |
result.Should().BeEquivalentTo(expected); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment