Created
September 14, 2012 02:11
-
-
Save Diullei/3719386 to your computer and use it in GitHub Desktop.
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.Text; | |
using System.Linq; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace TestProject1 | |
{ | |
[TestClass] | |
public class TestSmsDojo | |
{ | |
[TestMethod] | |
public void TestaSmsParser() | |
{ | |
const string expected = "77773367_7773302_222337777_777766606660366656667889999_9999555337777"; | |
var actual = new SmsParser("SEMPRE ACESSO O DOJOPUZZLES").Parse(); | |
Assert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void TestKeyboard() | |
{ | |
const string expected = "22"; | |
var keyboard = new Keyboard(); | |
var actual = keyboard.LocateChar("B"); | |
Assert.AreEqual(expected, string.Join("", actual)); | |
} | |
} | |
public class SmsParser | |
{ | |
private readonly Keyboard _keyboard = new Keyboard(); | |
private readonly char[] _message; | |
public SmsParser(string message) | |
{ | |
_message = message.ToCharArray(); | |
} | |
public string Parse() | |
{ | |
var parsedMsg = new StringBuilder(); | |
char[] last = null; | |
foreach (var m in _message.Select(t => _keyboard.LocateChar(t.ToString()))) | |
{ | |
if (last != null && m[0] == last[last.Length - 1]) | |
parsedMsg.Append("_"); | |
parsedMsg.Append(m); | |
last = m; | |
} | |
return parsedMsg.ToString(); | |
} | |
} | |
public class Keyboard | |
{ | |
private readonly string[] _keys = new[] | |
{ | |
" ", | |
"", | |
"ABC", | |
"DEF", | |
"GHI", | |
"JKL", | |
"MNO", | |
"PQRS", | |
"TUV", | |
"WXYZ" | |
}; | |
public char[] LocateChar(string character) | |
{ | |
var s = ""; | |
for (var i = 0; i < _keys.Length; i++) | |
{ | |
if (!_keys[i].Contains(character)) | |
continue; | |
for (var x = _keys[i].IndexOf(character); x >= 0; x--) | |
s += i; | |
break; | |
} | |
return s.ToCharArray(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment