Created
February 6, 2015 02:56
-
-
Save bembengarifin/9caeeb67aba9e9d55326 to your computer and use it in GitHub Desktop.
Top Coder Barclays
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 Microsoft.VisualStudio.TestTools.UnitTesting; | |
using System.Diagnostics; | |
namespace TopCoderBarclays | |
{ | |
[TestClass] | |
public class UnitTest1 | |
{ | |
[TestMethod] | |
public void TestMethod1() | |
{ | |
var scoreBoard = new Scoreboard(); | |
Assert.AreEqual(3000, scoreBoard.getScore("BBB")); | |
Assert.AreEqual(31300, scoreBoard.getScore("BbRJD")); | |
Assert.AreEqual(0, scoreBoard.getScore("")); | |
} | |
[TestMethod] | |
public void TestMethod2() | |
{ | |
var c = new TranspositionKey(); | |
CollectionAssert.AreEquivalent(new int[] { 1, 2, 3 }, c.makeKey("aaa")); | |
CollectionAssert.AreEquivalent(new int[] { 8, 7, 3, 2, 5, 1, 4, 6 }, c.makeKey("ywedkcjs")); | |
CollectionAssert.AreEquivalent(new int[] { 14, 20, 12, 18, 7, 19, 8, 2, 15, 1, 21, 3, 10, 11, 4, 22, 5, 16, 9, 13, 17, 6 }, c.makeKey("Quoth the raven, Nevermore.")); | |
} | |
[TestMethod] | |
public void TestMethod3() | |
{ | |
var c = new Spelling(); | |
Assert.AreEqual(0, c.distance(new[] { "topcoder" }, "topcoder")); | |
Assert.AreEqual(1, c.distance(new[] { "topcoder" }, "tppcoder")); | |
Assert.AreEqual(2, c.distance(new[] { "topcoder", "topcod" }, "topcode")); | |
Assert.AreEqual(-1, c.distance(new[] { "topcoderevent" }, "appirio")); | |
} | |
} | |
public class Spelling | |
{ | |
public int distance(string[] dictionary, string input) | |
{ | |
if (dictionary == null || dictionary.Length == 0) return -1; | |
var minDistance = int.MaxValue; | |
// evaluate per item in dictionary | |
foreach (var entry in dictionary) | |
{ | |
var distance = getDistance(entry, input); | |
if (distance < minDistance) | |
{ | |
minDistance = distance; | |
} | |
Debug.WriteLine(distance); | |
} | |
// return the lowest score | |
return minDistance; | |
} | |
private int getDistance(string entry, string input) | |
{ | |
if (Math.Abs(entry.Length - input.Length) > 1) return -1; | |
var maxLength = entry.Length < input.Length ? input.Length : entry.Length; | |
var distance = 0; | |
for (int i = 0; i < maxLength; i++) | |
{ | |
char cI = (i < input.Length) ? input[i] : '\0'; | |
char cD = (i < entry.Length) ? entry[i] : '\0'; | |
// different, needs to change +1 | |
// insert +2 | |
// delete +3 | |
if (cI == cD) | |
{ | |
} | |
else if (cI == '\0') | |
{ | |
distance += 2; | |
} | |
else if (cD == '\0') | |
{ | |
distance += 3; | |
} | |
else if (cI != cD) | |
{ | |
distance += 1; | |
} | |
} | |
// compare the same length first and apply the change | |
// to insert | |
return distance; | |
} | |
} | |
public class TranspositionKey | |
{ | |
public int[] makeKey(string input) | |
{ | |
//var counter = 0; | |
var positions = new int[input.Length]; | |
//System.Tuple<int, int> | |
var validChar = 0; | |
var lower = input.ToLower(); | |
for (int i = 0; i < lower.Length; i++) | |
{ | |
int c = lower[i]; | |
if (c >= 97 && c <= 122) | |
{ | |
positions[i] = c; | |
validChar++; | |
} | |
} | |
var cleansed = new int[validChar]; | |
var indexClean = 0; | |
foreach (var item in positions) | |
{ | |
if (item != 0) | |
{ | |
cleansed[indexClean] = item; | |
indexClean++; | |
} | |
} | |
var counter = 1; | |
var procesed = new int[validChar]; | |
for (int i = 97; i < 123; i++) | |
{ | |
for (int iC = 0; iC < cleansed.Length; iC++) | |
{ | |
if (cleansed[iC] == i) | |
{ | |
procesed[iC] = counter++; | |
} | |
} | |
} | |
return procesed; | |
} | |
} | |
public class Scoreboard | |
{ | |
public int getScore(string input) | |
{ | |
var score = 0; | |
foreach (var item in input) | |
{ | |
switch (item) | |
{ | |
case 'B': | |
score += 1000; | |
break; | |
case 'b': | |
score += 250; | |
break; | |
case 'R': | |
score += 50; | |
break; | |
case 'J': | |
score += 10000; | |
break; | |
case 'D': | |
score += 20000; | |
break; | |
default: | |
break; | |
} | |
} | |
return score; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment