Skip to content

Instantly share code, notes, and snippets.

@bobbychopra
Last active December 15, 2015 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobbychopra/5220453 to your computer and use it in GitHub Desktop.
Save bobbychopra/5220453 to your computer and use it in GitHub Desktop.
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