Skip to content

Instantly share code, notes, and snippets.

@fernandezjose
Last active July 1, 2017 03:05
Show Gist options
  • Save fernandezjose/19941a513e741accbe6e58e9ff094691 to your computer and use it in GitHub Desktop.
Save fernandezjose/19941a513e741accbe6e58e9ff094691 to your computer and use it in GitHub Desktop.
Exercise for Olo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace Inteview_Exercise_Toppings
{
class Program
{
static void Main(string[] args)
{
var httpClient = new HttpClient();
var response = httpClient.GetAsync("http://files.olo.com/pizzas.json").Result.Content.ReadAsStringAsync().Result;
var pizzaOrders = JsonConvert.DeserializeObject<List<Pizza>>(response);
Dictionary<List<string>, int> toppings = new Dictionary<List<string>, int>(new ToppingComparer());
pizzaOrders.ForEach(pizza =>
{
if (!toppings.ContainsKey(pizza.Toppings))
toppings.Add(pizza.Toppings, 1);
else
toppings[pizza.Toppings] += 1;
});
var top20 = toppings.OrderByDescending(x => x.Value).Take(20).ToList();
top20.ForEach(topping =>
{
Console.WriteLine($"{String.Join(",", topping.Key)} is number {top20.IndexOf(topping)+1} with {topping.Value} orders.");
});
}
}
class ToppingComparer : IEqualityComparer<List<string>>
{
public bool Equals(List<string> x, List<string> y)
{
return x.SequenceEqual(y);
}
public int GetHashCode(List<string> obj)
{
int hashcode = 0;
foreach (var item in obj)
{
hashcode ^= item.GetHashCode();
}
return hashcode;
}
}
class Pizza
{
public List<string> Toppings = new List<string>();
}
}
@fernandezjose
Copy link
Author

pepperoni is #1 with 4616 orders.
mozzarella cheese is #2 with 1014 orders.
four cheese is #3 with 956 orders.
bacon is #4 with 732 orders.
beef is #5 with 623 orders.
sausage is #6 with 402 orders.
italian sausage is #7 with 361 orders.
chicken is #8 with 229 orders.
ham is #9 with 165 orders.
mushrooms is #10 with 159 orders.
black olives is #11 with 117 orders.
pepperoni,four cheese is #12 with 103 orders.
alredo sauce is #13 with 101 orders.
four cheese,pepperoni is #14 with 100 orders.
mozzarella cheese,pepperoni is #15 with 96 orders.
cheddar cheese is #16 with 95 orders.
pineapple is #17 with 79 orders.
pepperoni,beef is #18 with 67 orders.
pepperoni,bacon is #19 with 63 orders.
pepperoni,mozzarella cheese is #20 with 59 orders.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment