Created
October 10, 2019 14:23
-
-
Save AndrewAllison/df160d780d894fdc691b331b2840c60b to your computer and use it in GitHub Desktop.
One of those questions from a technical test that gave me an itch to scratch.
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 NUnit.Framework; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace TestNinja.UnitTests | |
{ | |
public class Alphabetize | |
{ | |
public string FindLongestSequense(string sequence) | |
{ | |
var gatheredSequences = new List<string>(); | |
string matches = ""; | |
for (var i = 0; i < sequence.Length - 1; i++) | |
{ | |
// get the character | |
var thisCaharcter = sequence.ToCharArray()[i]; | |
var nextCaharcter = sequence.ToCharArray()[i + 1]; | |
// picking up the next character in the alphabet | |
var nextInTheAlphabet = (char)(thisCaharcter + 1); | |
if (nextCaharcter == nextInTheAlphabet) | |
{ | |
if (matches.Length <= 0) | |
matches += $"{thisCaharcter.ToString()}{nextCaharcter.ToString()}"; | |
else | |
matches += nextCaharcter.ToString(); | |
} | |
else | |
{ | |
if (matches.Length > 1) | |
{ | |
gatheredSequences.Add(matches); | |
} | |
matches = ""; | |
} | |
} | |
return gatheredSequences.OrderByDescending(s => s.Length).First(); | |
} | |
} | |
[TestFixture] | |
class AlphabetTests | |
{ | |
[Test] | |
public void FindLongestPatter_WithASimplePatter_ReturnsCorrectResult() | |
{ | |
var alphabetize = new Alphabetize(); | |
var stringItem = "xxxwxyasdkajdhakfksfjh"; | |
var result = alphabetize.FindLongestSequense(stringItem); | |
Assert.That(result, Is.EqualTo("wxy")); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment