Created
October 16, 2017 19:03
-
-
Save rglos/93148edc503e0c736af94bfbf867a27b to your computer and use it in GitHub Desktop.
Interview Assignment
This file contains 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 Newtonsoft.Json; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace PopularBikes | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var stopwatch = System.Diagnostics.Stopwatch.StartNew(); | |
// #1 - get the json as a string | |
var getJsonStopwatch = System.Diagnostics.Stopwatch.StartNew(); | |
Console.WriteLine("Getting JSON..."); | |
string surveyJson = null; | |
using (var webClient = new WebClient()) | |
{ | |
surveyJson = webClient.DownloadString("https://trekhiringassignments.blob.core.windows.net/interview/bikes.json"); | |
} | |
if (surveyJson == null || string.IsNullOrEmpty(surveyJson)) | |
{ | |
Console.WriteLine("JSON null or empty"); // we could do other checks before going to step 2 if needing (i.e. is the JSON valid, in the manner we expect, etc) | |
return; | |
} | |
getJsonStopwatch.Stop(); | |
Console.WriteLine($"Getting JSON - complete, Elapsed: {getJsonStopwatch.Elapsed}"); | |
// #2 - deserialize the json into a nice object we can use to group, count and sort with LINQ | |
Console.WriteLine(); | |
Console.WriteLine("Deserializing JSON"); | |
List<SurveyResponse> surveyResponses = JsonConvert.DeserializeObject<List<SurveyResponse>>(surveyJson); | |
Console.WriteLine($"Found {surveyResponses.Count:n0} survery responses..."); | |
// #3 - group, count, sort with LINQ | |
// We need a comparer to compare the string of bikes, I'm leaving my sample and testing code for references on why I created a SurveyResponseComparer | |
// | |
//var list1 = new List<string> { "A", "B" }; | |
//var list2 = new List<string> { "A", "B" }; | |
//var test = list1.OrderBy(x => x).SequenceEqual(list2.OrderBy(x => x)); | |
// | |
//var list3 = new List<string> { "B", "A" }; | |
//var test2 = list1.OrderBy(x => x).SequenceEqual(list3.OrderBy(x => x)); | |
// | |
//var groupQuery = surveyResponses.GroupBy(x => x.Bikes.ToArray()); | |
//var groupQuery = surveyResponses.GroupBy(x => x).Select(group => new { Group = group.Key, Count = group.Count() }); | |
//var groupQuery = surveyResponses.GroupBy(x => x.Bikes, new SurveyResponseComparer()); | |
// | |
var groupQuery = surveyResponses.GroupBy(x => x.Bikes, new SurveyResponseComparer()).Select(group => new { Group = group.Key, Count = group.Count() }); | |
var sortedQuery = groupQuery.OrderByDescending(x => x.Count); | |
// #4 - only take the top 20 | |
var topTwentyQuery = sortedQuery.Take(20); | |
// #5 - show the results | |
Console.WriteLine(); | |
Console.WriteLine("Top 20 most popular responses:"); | |
var number = 0; | |
foreach (var item in topTwentyQuery) | |
{ | |
number += 1; | |
Console.WriteLine($"Number {number} with {item.Count:n0} responses: {string.Join(", ", item.Group)}"); | |
} | |
stopwatch.Stop(); | |
Console.WriteLine(); | |
Console.WriteLine($"Main - complete, Elapsed: {stopwatch.Elapsed}"); | |
} | |
} | |
class SurveyResponse | |
{ | |
public List<string> Bikes { get; set; } | |
} | |
class SurveyResponseComparer : IEqualityComparer<List<string>> | |
{ | |
public bool Equals(List<string> x, List<string> y) | |
{ | |
// We don't care about order (i.e. {Bike1, Bike2} should equal {Bike2, Bike1}) so we can use SequenceEqual since it cares about order, so let's order the lists before we compare | |
var xOrdered = x.OrderBy(s => s); | |
var yOrdered = y.OrderBy(s => s); | |
return xOrdered.SequenceEqual(yOrdered); | |
} | |
public int GetHashCode(List<string> obj) | |
{ | |
var ordered = obj.OrderBy(s => s); | |
return string.Join(",", ordered).GetHashCode(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This would produce the following output as of 2017.10.16: