Posted in:

I do regular lunchtime developer training sessions at my work, and this week I created a short quiz for the developers to try out their LINQ skills. They are not too hard (although number 5 is quite tricky) and most are based on actual problems I needed to solve recently. I thought I’d post them here too in case anyone fancies a short brain-teaser. If you attempt it, why not post your answers to a Github gist and link to them in the comments. I’ll hopefully do a followup post with some thoughts on how I would attempt these. Also, feel free to suggest additional interesting LINQ problems of your own…

The Challenge…

Each of these problems can be solved using a single C# statement by making use of chained LINQ operators (although you can use more statements if you like). You'll find the String.Split function helpful to get started on each problem. Other functions you might need to use at various points are String.Join, Enumerable.Range, Zip, Aggregate, SelectMany. LINQPad would be a good choice to try out your ideas.

  1. Take the following string "Davis, Clyne, Fonte, Hooiveld, Shaw, Davis, Schneiderlin, Cork, Lallana, Rodriguez, Lambert" and give each player a shirt number, starting from 1, to create a string of the form: "1. Davis, 2. Clyne, 3. Fonte" etc

  2. Take the following string "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" and turn it into an IEnumerable of players in order of age (bonus to show the age in the output)

  3. Take the following string "4:12,2:43,3:51,4:29,3:24,3:14,4:46,3:25,4:52,3:27" which represents the durations of songs in minutes and seconds, and calculate the total duration of the whole album

  4. Create an enumerable sequence of strings in the form "x,y" representing all the points on a 3x3 grid. e.g. output would be: 0,0 0,1 0,2 1,0 1,1 1,2 2,0 2,1 2,2

  5. Take the following string "00:45,01:32,02:18,03:01,03:44,04:31,05:19,06:01,06:47,07:35" which represents the times (in minutes and seconds) at which a swimmer completed each of 10 lengths. Turn this into an IEnumerable of TimeSpan objects containing the time taken to swim each length (e.g. first length was 45 seconds, second was 47 seconds etc)

  6. Take the following string "2,5,7-10,11,17-18" and turn it into an IEnumerable of integers: 2 5 7 8 9 10 11 17 18

Want to learn more about LINQ? Be sure to check out my Pluralsight course LINQ Best Practices.

Comments

Comment by nilp

1: http://ideone.com/nDGY5r
2: http://ideone.com/FrcODC
3: http://ideone.com/jaZpOD
4: http://ideone.com/qmG5cp
5: http://ideone.com/QZxpcQ
6: http://ideone.com/IEXDK1 //ok, too tired to thinkg about merging it

nilp
Comment by Mark Jones

1-4 here https://gist.github.com/orangutanboy/5870892

I'm happy with 1 and 4 but the others feel clunky.

Comment by Mark H

thanks for joining in nilp and Mark, I've done a followup post with answers to them all.

Comment by Mohamed Elshawaf

After I'm done with all challenges
I noticed that this was 5 years ago,anyway here are my attempts, it was fun:
https://gist.github.com/msh...

Mohamed Elshawaf
Comment by Mark Heath

great work, glad you enjoyed it

Mark Heath
Comment by Carrandas

I know it's old but I gave it a go, fun challenge.
https://gist.github.com/Car...
The fifth solultion seems rather ugly as I could not find a way to re-use the Timespan list.

Carrandas
Comment by Chris S

The lunchtime challenges have been fun. I just wanted to post my answer to #2.
"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"
.Split(";")
.Select(i => i.Split(", "))
.Select(player => new { Player = player.First(),
DOB = DateTime.ParseExact(player.Last(), "dd/MM/yyyy",
System.Globalization.CultureInfo.InvariantCulture),
Age = ((int.Parse(DateTime.Now.ToString("yyyyMMdd")) -
int.Parse(DateTime.ParseExact(player.Last(), "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture).ToString("yyyyMMdd"))) / 10000) })
.OrderBy(age => age.DOB)

Chris S