Created
February 6, 2016 12:22
-
-
Save archer884/ce2a41c0f10f12d86fa2 to your computer and use it in GitHub Desktop.
Airport delays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using CsvHelper; | |
using CsvHelper.Configuration; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
namespace AirlineDelays | |
{ | |
class FlightRecord | |
{ | |
public string Carrier { get; set; } | |
public string Origin { get; set; } | |
public string Destination { get; set; } | |
public int? DepartureDelay { get; set; } | |
public int? ArrivalDelay { get; set; } | |
public bool Cancelled { get; set; } | |
public double Distance { get; set; } | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
#if DEBUG | |
args = new[] | |
{ | |
"airline-on-time-performance-sep2014-us.csv", | |
"Dallas/Fort Worth TX", | |
"Houston TX", | |
}; | |
#endif | |
var filePath = args[0]; | |
var origin = args[1]; | |
var destination = args[2]; | |
using (var stream = File.OpenText(filePath)) | |
{ | |
var flightRecords = ParseFlightRecords(stream).ToList(); | |
var flightRecordsByCarrier = flightRecords | |
.Where(record => MatchingFlight(record, origin, destination)) | |
.GroupBy(record => record.Carrier) | |
.ToList(); | |
PrintDelays(flightRecordsByCarrier); | |
PrintWorstAirports(flightRecords); | |
} | |
} | |
static void PrintDelays(IEnumerable<IGrouping<string, FlightRecord>> carriers) | |
{ | |
foreach (var carrier in carriers) | |
{ | |
var maxDelay = 0; | |
var averageDelay = carrier | |
.Select(record => | |
{ | |
if (record.ArrivalDelay > maxDelay) | |
{ | |
maxDelay = record.ArrivalDelay.Value; | |
} | |
return record.ArrivalDelay; | |
}).Average(); | |
Console.WriteLine("Carrier: {0}\nDelay (average/max): {1:F1}/{2}\n", | |
carrier.Key, | |
averageDelay, | |
maxDelay); | |
} | |
} | |
static void PrintWorstAirports(IEnumerable<FlightRecord> records) | |
{ | |
var worstDestination = records | |
.GroupBy(record => record.Destination) | |
.Select(group => new | |
{ | |
City = group.Key, | |
AverageDelay = group.Average(record => record.ArrivalDelay) | |
}) | |
.OrderBy(city => city.AverageDelay) | |
.Last(); | |
Console.WriteLine("Worst destination: {0}", worstDestination); | |
var worstOrigin = records | |
.GroupBy(record => record.Origin) | |
.Select(group => new | |
{ | |
City = group.Key, | |
AverageDelay = group.Average(record => record.DepartureDelay) | |
}) | |
.OrderBy(city => city.AverageDelay) | |
.Last(); | |
Console.WriteLine("Worst origin: {0}", worstOrigin); | |
} | |
static bool MatchingFlight(FlightRecord record, string origin, string destination) | |
{ | |
return string.Equals(record.Origin, origin, StringComparison.OrdinalIgnoreCase) | |
&& string.Equals(record.Destination, destination, StringComparison.OrdinalIgnoreCase); | |
} | |
static IEnumerable<FlightRecord> ParseFlightRecords(TextReader content) | |
{ | |
var reader = new CsvReader(content, GetConfiguration()); | |
return reader.GetRecords<FlightRecord>(); | |
} | |
static CsvConfiguration GetConfiguration() | |
{ | |
var config = new CsvConfiguration(); | |
config.RegisterClassMap(GetClassMap()); | |
return config; | |
} | |
static CsvClassMap<FlightRecord> GetClassMap() | |
{ | |
var map = new DefaultCsvClassMap<FlightRecord>(); | |
map.Map(record => record.Carrier).Index(0); | |
map.Map(record => record.Origin).Index(1); | |
map.Map(record => record.Destination).Index(2); | |
map.Map(record => record.DepartureDelay).Index(3); | |
map.Map(record => record.ArrivalDelay).Index(4); | |
map.Map(record => record.Cancelled).Index(5); | |
map.Map(record => record.Distance).Index(6); | |
return map; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment