Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Answers to Mark Heath's Lunchtime Linq Challenge 2
var startX = 'C';
var startY = 5;
var diagonals = Enumerable.Range(65, 8)
.SelectMany(x => Enumerable.Range(1, 8), (x, y) => new { x = (char)x, y })
.Where(coord =>
((coord.x - startX == coord.y - startY))
|| (startX - coord.x == coord.y - startY)
|| (startX - coord.x == coord.y + startY)
|| (coord.x - startX == coord.y + startY))
.Select(coord => coord.x.ToString() + coord.y.ToString())
.Except(new[] { startX.ToString() + startY });
"10,5,0,8,10,1,4,0,10,1".
Split(',').
Select(int.Parse).
OrderBy(s => s).
Skip(3).
Sum()
"Dog,Cat,Rabbit,Dog,Dog,Lizard,Cat,Cat,Dog,Rabbit,Guinea Pig,Dog".
Split(',').
GroupBy(s => s).
ToDictionary(g => g.Key, g => g.Count())
string.Join("",
Enumerable.Range(0, Regex.Matches("A5B10CD3", @"\D").Count)
.Select(i => Regex.Matches("A5B10CD3", @"[A-Za-z]\d*")[i].Groups[0].Value)
.Select(match => new { @char = match[0], count = match.Substring(1) })
.Select(x => new string(x.@char, x.count == "" ? 1 : int.Parse(x.count))))
"0,6,12,18,24,30,36,42,48,53,58,63,68,72,77,80,84,87,90,92,95,96,98,99,99,100,99,99,98,96,95,92,90,87,84,80,77,72,68,63,58,53,48,42,36,30,24,18,12,6,0,-6,-12,-18,-24,-30,-36,-42,-48,-53,-58,-63,-68,-72,-77,-80,-84,-87,-90,-92,-95,-96,-98,-99,-99,-100,-99,-99,-98,-96,-95,-92,-90,-87,-84,-80,-77,-72,-68,-63,-58,-53,-48,-42,-36,-30,-24,-18,-12,-6".
Split(',').
Select((s, i) => i % 5 == 4 ? s : null).
Where(s => !string.IsNullOrEmpty(s)).
Select(int.Parse)
"Yes,Yes,No,Yes,No,Yes,No,No,No,Yes,Yes,Yes,Yes,No,Yes,No,No,Yes,Yes".
Split(',').
Sum(v => v == "Yes" ? 1 : -1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.