Ejemplo n.º 1
0
        public Dictionary <string, char> Calc(IEnumerable <string> words)
        {
            Dictionary <string, char> keys = new();
            var           shorts           = words.ToDictionary(w => w, w => calculator.Calc(w).ToList());
            var           chosen           = new HashSet <char>();
            List <string> toRemove;

            while (shorts.Count != 0)
            {
                toRemove = new List <string>();
                foreach (var key in shorts.Keys.OrderBy(x => x, comparator))
                {
                    if (shorts[key].Count == 0)
                    {
                        toRemove.Add(key);
                        continue;
                    }
                    char first = shorts[key].First();
                    if (!chosen.Contains(first))
                    {
                        chosen.Add(first);
                        keys[key] = first;
                        toRemove.Add(key);
                    }
                    shorts[key].RemoveAt(0);
                }
                foreach (var key in toRemove)
                {
                    shorts.Remove(key);
                }
            }
            return(keys);
        }
Ejemplo n.º 2
0
 public WordPool(ShortCalculator calculator, IEnumerable <string> words = null)
 {
     this.calculator = calculator;
     this.words      = words?.ToHashSet() ?? new();
     this.keys       = words.ToDictionary(w => w, w => calculator.Calc(w).ToHashSet());
 }