Skip to content

Instantly share code, notes, and snippets.

@Carrandas
Created July 19, 2018 12:47
SoundCode Lunchtime LINQ Challenge
//Linq challenge #1.1
void Main()
{
var s = "Davis, Clyne, Fonte, Hooiveld, Shaw, Davis, Schneiderlin, Cork, Lallana, Rodriguez, Lambert";
var names = s
.Split(',')
.Select((x,i) => (i+1) + ":" + x)
.Aggregate((x,y) => x + ", " + y)
.Dump();
}
//Linq challenge #1.2
void Main()
{
var s = "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 names = s
.Split(';')
.Select(x => x.Split(','))
.Select(x => new {Name = x[0], Birth = DateTime.Parse(x[1])})
.OrderByDescending(x => x.Birth)
.Select(x => x.Name + " - " + (int)(DateTime.Now - x.Birth).TotalDays / 365)
.Dump();
}
//Linq challenge #1.3
void Main()
{
var s = "4:12,2:43,3:51,4:29,3:24,3:14,4:46,3:25,4:52,3:27";
s
.Split(',')
.Select(x => TimeSpan.Parse("00:" + x).TotalSeconds)
.Sum()
.Dump();
}
//Linq challenge 1.4
from i in Enumerable.Range(0,3)
from j in Enumerable.Range(0,3)
select "(" + i + "," + j + ")"
.Dump()
//Linq challenge #1.5
void Main()
{
string s = "00:45,01:32,02:18,03:01,03:44,04:31,05:19,06:01,06:47,07:35";
var times =
s
.Split(',')
.Select(x => TimeSpan.Parse("00:" + x));
var timesPerLap =
times.Take(1)
.Concat
(
times
.Zip(times.Skip(1), (x,n) => (n-x))
)
.Dump();
}
void Main()
{
var s = "2,5,7-10,11,17-18";
s
.Split(',')
.SelectMany(x =>
{
var a = x.Split('-');
var p = int.Parse(a[0]);
var n = a.Count() == 2 ? int.Parse(a[1]) : p;
return Enumerable.Range(p, n - p + 1);
})
.Dump();
}
// Define other methods and classes here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment