Advertisement
Guest User

Lunchtime LINQ Challenge #2 - JAH Answers

a guest
Jul 26th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.58 KB | None | 0 0
  1. /* Problem 1 – Motor Sport Scores */
  2. "10,5,0,8,10,1,4,0,10,1".Split(',').Select(v=>int.Parse(v)).OrderBy(s=>s).Skip(3).Aggregate((s, next) => next + s)
  3.  
  4. /* Problem 2 – Bishop Moves */
  5. Enumerable.Range(0, 8).Join(Enumerable.Range(1, 8), x=>0, x=>0, (x, y)=> new {L = x, N = y} ).Where(T=>(Math.Abs(T.L-('C'-'A'))-Math.Abs(T.N-6)) == 0).Where(T=>!(T.L == ('C'-'A') && T.N == 6)).Select(T=>((char)(T.L+'A')).ToString()+T.N)
  6.  
  7. /* Problem 3 – Sampling */
  8. "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(v=>int.Parse(v)).Where((e, i)=>(i+1)%5==0)
  9.  
  10. /* Problem 4 – Vote Winning Margin */
  11. "Yes,Yes,No,Yes,No,Yes,No,No,No,Yes,Yes,Yes,Yes,No,Yes,No,No,Yes,Yes".Split(',').GroupBy(yn=>yn).Select(g=>g.Count()).OrderBy(c=>c).Aggregate((v, next)=>next-v)
  12.  
  13. /* Problem 5 – Counting Pets */
  14. string.Join(" ", "Dog,Cat,Rabbit,Dog,Dog,Lizard,Cat,Cat,Dog,Rabbit,Guinea Pig,Dog".Split(',').Select(p=>((new []{ "Dog", "Cat" }).Contains(p))?p:"Other").GroupBy(p=>p).Select(pg=>pg.First() + ":" + pg.Count()))
  15.  
  16. /* Problem 6 – Run Length Decoding */
  17. string.Join("", (new Regex("[A-Z]\\d*").Matches("A5B10CD3")).Cast<Match>().Select(m=>m.Value).Select(m=>new {Letter = m.Substring(0, 1), Count = (m.Length > 1)?int.Parse(m.Substring(1, m.Length-1)):1}).Select(p=>new String(p.Letter[0], p.Count)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement