Last active
December 15, 2015 06:59
-
-
Save bobbychopra/5220453 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
void Main() | |
{ | |
Console.WriteLine(GetCode("A")); | |
Console.WriteLine(GetCode("Z")); | |
Console.WriteLine(GetCode("AA")); | |
Console.WriteLine(GetCode("AZ")); | |
Console.WriteLine(GetCode("AAA")); | |
} | |
// Define other methods and classes here | |
int GetCode(char c) | |
{ | |
return (int)c - 64; | |
} | |
int GetCode(string str) | |
{ | |
var val = str.ToUpper(); | |
var ind = val.Reverse().Select( (c,i) => new { Index = i, Code = c }).ToList(); | |
return ind.Aggregate(0, (i, x) => | |
{ | |
//NOTE: For Debugging | |
//Console.WriteLine("Code: {0} Index: {1} Prev:{2}, ", x.Code, x.Index, i); | |
var sum = i + GetCode(x.Code)*(int)Math.Pow(26,x.Index); | |
return sum; | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment