I’ve got another LINQ Challenge for you (The first one is here with C# answers here, and F# answers here). The idea with these challenges is to see if you can solve them using LINQ. It is in fact possible to solve them all in a single LINQ statement, but of course that may not always make for the most readable code, so feel free to break your answer up into multiple statements if you prefer.

So here’s problems for this challenge…

Problem 1 – Motor Sport Scores

In a motor sport competition, a player's points total for the season is the sum of all the points earned in each race, but the three worst results are not counted towards the total. Calculate the following player's score based on the points earned in each round:


Problem 2 – Bishop Moves

A chess board is an 8x8 grid, from a1 in the bottom left to h8 in the top right. A bishop can travel diagonally any number of squares. So for example a bishop on c5 can go to b4 or to a3 in one move. Create an enumerable sequence of board positions that includes every square a bishop can move to in one move on an empty chess board, if its starting position is c6. e.g. output would include b7, a8, b5, a4

Problem 3 – Sampling

The following sequence has 100 entries. Sample it by taking every 5th value and discarding the rest. The output should begin with 24,53,77,...


Problem 4 – Vote Winning Margin

Yes won the vote, but how many more Yes votes were there than No votes?


Problem 5 – Counting Pets

Count how many have dogs, how many have cats, and how many have other pets. e.g. output would be a structure or sequence containing: Dog:5 Cat:3 Other:4

"Dog,Cat,Rabbit,Dog,Dog,Lizard,Cat,Cat,Dog,Rabbit,Guinea Pig,Dog"

Problem 6 – Run Length Decoding

Expand strings in this form: A5B10CD3 to AAAAABBBBBBBBBBCDDD. Note that a letter may have 0 digits after it (C in the example), which means just repeat it once. It may also have more than one digit after it, such as B in this example, which is repeated 10 times. This one is a bit tricky, but there is a handy method in the .NET framework (not part of LINQ) which can help you get started.


Over to You

Hope you have fun solving these. If you want to participate in the challenge, put your answers into a GitHub Gist and leave a link to it in the comments below.

I recommend LINQPad as a great tool for working on your answers, and you’ll find some additional .NET framework methods like string.Split and string.Join helpful in places.

I’ll give people a week to get their answers in, and then I’ll post what I consider to be the best solutions in both C# and F#. Of course, feel free to solve these in whatever language you like – Python, PowerShell, Perl…

Want to learn more about LINQ? Be sure to check out my Pluralsight course More Effective LINQ.
Vote on HN
comments powered by Disqus