Created
July 2, 2020 07:09
-
-
Save appcoreopc/6517b4e92cb6fa5a5d456867b3186e6b to your computer and use it in GitHub Desktop.
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 System.Collections.Generic; | |
using System.Linq; | |
namespace Olo | |
{ | |
public class TopPizzaSelector : ITopSelector | |
{ | |
private const string Separator = ","; | |
Dictionary<string, int> DataMapper = new Dictionary<string, int>(); | |
public void Process(IEnumerable<PizzaModel> model) | |
{ | |
foreach (var item in model) | |
{ | |
var key = string.Join(Separator, item.Toppings); | |
MapCountItem(key.ToLower()); | |
} | |
} | |
public IEnumerable<KeyValuePair<string,int>> GetTop(int topItem) | |
{ | |
return DataMapper.OrderByDescending(x => x.Value).Take(topItem); | |
} | |
private void MapCountItem(string key, int count = 1) | |
{ | |
if (DataMapper.TryGetValue(key, out int currentValue)) | |
DataMapper[key] = currentValue + count; | |
else | |
DataMapper.TryAdd(key, count); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment