Skip to content

Instantly share code, notes, and snippets.

@a5ync
Created March 24, 2017 07:10
Show Gist options
  • Save a5ync/82a67cd96c7a3119cf02dcf999312ecd to your computer and use it in GitHub Desktop.
Save a5ync/82a67cd96c7a3119cf02dcf999312ecd to your computer and use it in GitHub Desktop.
Average city temperatures in CS
class Program
{
public class CityTemps
{
public string City { get; set; }
public string Year { get; set; }
public double Temp { get; set; }
}
static void Main(string[] args)
{
var temps = new string[] {
"Denver,2011,10",
"Denver,2012,20",
"Denver,2013,30",
"Denver,2014,40",
"Vegas,2000,100",
"Vegas,2001,98",
"Reno,2001,50" };
var list = new List<CityTemps>();
temps.ToList().ForEach(k =>
{
var segs = k.Split(",".ToCharArray());
list.Add(new CityTemps()
{
City = segs[0],
Year = segs[1],
Temp = Double.Parse(segs[2])
});
});
var p = list.GroupBy(g => g.City)
.Select(g => new
{
City = g.Key,
AvarageTemp = g.Average(pg => pg.Temp)
}).OrderBy(o => o.City);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment