Last active
July 19, 2018 07:23
-
-
Save mshwf/8902b0c603fcb2228602911dc2c5d7cf to your computer and use it in GitHub Desktop.
My attempt to solve Lunchtime LINQ Challenge part1 https://www.markheath.net/post/lunchtime-linq-challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//challeng 1 | |
string names = "Davis, Clyne, Fonte, Hooiveld, Shaw, Davis, Schneiderlin, Cork, Lallana, Rodriguez, Lambert"; | |
var team = names.Split(',').Select((player, index) => (index + 1) + ". " + player.Trim()).Aggregate((curr, next) => curr + ", " + next); | |
//challenge 2 (see answer) | |
string playersData = "Jason Puncheon, 26/06/1986; Jos Hooiveld, 22/04/1983; Kelvin Davis, 29/09/1976; Luke Shaw, 12/07/1995; " + | |
"Gaston Ramirez, 02/12/1990; Adam Lallana, 10/05/1988"; | |
var players = playersData.Split(';').Select(player => (Name: player.Trim().Substring(0, player.IndexOf(',') - 1), DoB: DateTime.ParseExact(player.Substring(player.IndexOf(',') + 1).Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture))).Select(x => new { x.Name, x.DoB, Age = DateTime.Now.Year - x.Item2.Year }).OrderBy(x => x.Age).ToList(); | |
//challenge 3 (see answer) | |
string durations = "4:12,2:43,3:51,4:29,3:24,3:14,4:46,3:25,4:52,3:27"; | |
var timeOfAll = durations.Split(',').Select(dur => | |
{ | |
var time = dur.Split(':'); | |
return TimeSpan.FromMinutes(int.Parse(time[0])) + TimeSpan.FromSeconds(int.Parse(time[1])); | |
}).Aggregate((cur, next) => cur + next); | |
//challenge 4 (see answer) | |
int[] pts = new int[] { 0, 1, 2 }; | |
string grid = (from x in pts from y in pts select x + "," + y).Aggregate((a, b) => a + " " + b); | |
//challenge 5 | |
string durations = "00:45,01:32,02:18,03:01,03:44,04:31,05:19,06:01,06:47,07:35"; | |
var timeSpans = durations.Split(',').Select(x => | |
{ | |
var span = x.Split(':'); | |
var min = span[0]; | |
var sec = span[1]; | |
return TimeSpan.FromMinutes(double.Parse(min)) + TimeSpan.FromSeconds(double.Parse(sec)); | |
}); | |
var lengths = timeSpans.Zip(timeSpans.Prepend(TimeSpan.FromSeconds(0)), (x, y) => x - y); | |
//challenge 6 | |
string input = "2,5,7-10,11,17-18"; | |
List<int> nums = new List<int>(); | |
foreach (var item in input.Split(',')) | |
{ | |
if (item.Contains('-')) | |
{ | |
var bounds = item.Split('-'); | |
var lower = int.Parse(bounds[0]); | |
var upper = int.Parse(bounds[1]); | |
for (int i = lower; i <= upper; i++) | |
{ | |
nums.Add(i); | |
} | |
} | |
else | |
nums.Add(int.Parse(item)); | |
} | |
//answers | |
https://www.markheath.net/post/lunchtime-linq-challenge-answers |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment