Skip to content

Instantly share code, notes, and snippets.

@jamescurran
Last active July 18, 2018 05:36
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 jamescurran/af9c77c25ef870519d331a9a402aaa54 to your computer and use it in GitHub Desktop.
Save jamescurran/af9c77c25ef870519d331a9a402aaa54 to your computer and use it in GitHub Desktop.
Mark Heath's LINQ Challenge #3 -- https://markheath.net/post/linq-challenge-3
"1,2,1,1,0,3,1,0,0,2,4,1,0,0,0,0,2,1,0,3,1,0,0,0,6,1,3,0,0,0"
.Split(',')
.Select(digit=>digit=="0") // list of true/false
.Segment((curr,prev, _)=>curr!=prev) // segments of true/false
.Where(seg=>seg.FirstOrDefault()) // remove falses
.Max(tr=>tr.Count()) // find longest segment
"4♣ 5♦ 6♦ 7♠ 10♥;10♣ Q♥ 10♠ Q♠ 10♦;6♣ 6♥ 6♠ A♠ 6♦;2♣ 3♥ 3♠ 2♠ 2♦;2♣ 3♣ 4♣ 5♠ 6♠"
.Split(';')
.Select(h=>(Hand:h, Cards:h.Split(' ').GroupBy(c =>c.Substring(0,c.Length-1))))
.Where(h=>h.Cards.Count() == 2) // A full house will have only two ranks
.Where(h =>h.Cards.First().Count() == 2 || h.Cards.First().Count() == 3)
.Select(h=>h.Hand)
String.Join(",", Enumerable.Range(2018, 10).Select(y=>new DateTime(y, 12,25).DayOfWeek.ToString()))
"parts,traps,arts,rats,starts,tarts,rat,art,tar,tars,stars,stray,ssss"
.Split(',')
.Where(ca=>ca.Length == target.Length && ca.All(c => target.Contains(c)))
.Where(ca=>target.All(t=>ca.Contains(t)))
"Santi Cazorla, Per Mertesacker, Alan Smith, Thierry Henry, Alex Song, Paul Merson, Alexis Sánchez, Robert Pires, Dennis Bergkamp, Sol Campbell"
.Split(',')
.Select(n=>(parts:n.Trim().Split(' '), name:n))
.Select(tp=>(initial:tp.parts[0][0].ToString()+tp.parts[1][0].ToString(), tp.name))
.GroupBy(tp =>tp.initial, tp=>tp.name)
.Where(g=>g.Count() > 1)
.Select(g=> g.Key + " ==> " + String.Join(",", g))
// This solution makes the assumption that the first edit starts at time 00:00, and
// the last edit ends before 2:00:00, which is true for the given test data,
// but may not be for the general case.
"0:00:00-0:00:05;0:55:12-1:05:02;1:37:47-1:37:51"
.Split(';')
.SelectMany(c => c.Split('-'))
.Select(c => TimeSpan.Parse(c))
.Skip(1)
.Concat(new TimeSpan[1] { TimeSpan.FromHours(2) })
.Segment((ts, inx)=> (inx & 1)==0)
.Select(b => $"{b.First()}-{b.Last()}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment