Skip to content

Instantly share code, notes, and snippets.

@burlistic
Last active August 29, 2015 14:26
Show Gist options
  • Save burlistic/9d7730caa5e408bddcb2 to your computer and use it in GitHub Desktop.
Save burlistic/9d7730caa5e408bddcb2 to your computer and use it in GitHub Desktop.
/* 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 */
var arrayPlayers = "Davis, Clyne, Fonte, Hooiveld, Shaw, Davis, Schneiderlin, Cork, Lallana, Rodriguez, Lambert".Split(',');
// Query Style
//var i = 1;
//var queryPlayers = from player in arrayPlayers
// select i++ + " " + player;
// Fluent style using Zip and Range
var queryPlayers = arrayPlayers.Zip(Enumerable.Range(1,10), (first, second) => second + " " + first);
//queryPlayers.Dump();
/* 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) */
var playersWithBDay = "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(';');
// split name and bday and create a collection of objects
var sortedPlayerObjects = playersWithBDay.Select(p =>
new {
Name = p.Split(',')[0].Trim(),
BDay = DateTime.Parse(p.Split(',')[1])
}).OrderByDescending(pl => pl.BDay);
//sortedPlayerObjects.Dump();
/* 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 */
var albumTimes = "4:12,2:43,3:51,4:29,3:24,3:14,4:46,3:25,4:52,3:27".Split(',');
var totalMinutes = albumTimes.Sum(a => int.Parse(a.Substring(0, a.IndexOf(":")))) +
albumTimes.Sum(a => int.Parse(a.Substring(a.IndexOf(":") + 1, a.Length - a.IndexOf(":") - 1))) / 60;
var stringTimeIncRemainingSecords = totalMinutes + "." + (albumTimes.Sum(a => int.Parse(a.Substring(a.IndexOf(":") + 1, a.Length - a.IndexOf(":") - 1))) % 60).ToString();
//stringTimeIncRemainingSecords.Dump();
// TODO - improve? Make one query?
/* 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 */
int[] seed0 = new [] {0,0,0};
int[] seed1 = new [] {1,1,1};
int[] seed2 = new [] {2,2,2};
var seed = Enumerable.Range(0,3);
var comb0 = seed0.Zip(seed, (first, second) => first + "," + second);
var comb1 = seed1.Zip(seed, (first, second) => first + "," + second);
var comb2 = seed2.Zip(seed, (first, second) => first + "," + second);
string[] comb = comb0.Concat(comb1).Concat(comb2).ToArray();
//comb.Dump();
// Nicer example from https://gist.github.com/orangutanboy/5870892
var s = (from x in Enumerable.Range(0, 3)
from y in Enumerable.Range(0, 3)
select x + "," + y);
//s.Dump();
/* 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 enumerable of timespan objects containing the time taken to swim each length
(e.g. first length was 45 seconds, second was 47 seconds etc) */
var times = "00:45,01:32,02:18,03:01,03:44,04:31,05:19,06:01,06:47,07:35".Split(',');
var timespans = times.Select(t => new TimeSpan(0, int.Parse(t.Split(':')[0]), int.Parse(t.Split(':')[1])).TotalSeconds);
//timespans.Dump();
/* 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 */
var numbers = "2,5,7-10,11,17-18".Split(',');
var numberProcessed = numbers.Select (n => n.Contains("-") ? Enumerable.Range(int.Parse(n.Split('-')[0]), int.Parse(n.Split('-')[1]) - int.Parse(n.Split('-')[0]) + 1) : Enumerable.Range(int.Parse(n),1));
// Join into one arry
numberProcessed.SelectMany (p => p ).ToArray().Dump();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment