Skip to content

Instantly share code, notes, and snippets.

@robhol
Last active October 23, 2015 22:21
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 robhol/20b7c8f24f8658ae6e61 to your computer and use it in GitHub Desktop.
Save robhol/20b7c8f24f8658ae6e61 to your computer and use it in GitHub Desktop.
int impl(string str)
{
var sum = 0;
var last = 0;
for (int i=0; i < str.Length; i++)
{
var val = values[ str[i] ];
sum += val;
if (i > 0 && last < val)
sum -= 2 * last;
last = val;
}
return sum;
}
//bonus: functional-style C# using LINQ
inp => (" " + inp)
.Zip(inp, (pc, c) => new[] {pc != ' ' ? values[pc] : 0, values[c] })
.Sum(x => x[1] - 2 * ( x[0] < x [1] ? x[0] : 0 ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment