Example #1
0
        public static List <Person> FromString(string contents)
        {
            var lines = contents.Replace("\r", "").Split("\n");
            var names = lines.First().Split('\t').Skip(1);
            var N     = names.Count();

            var preferences = lines.Skip(1);

            var people = new List <Person>();

            foreach (var name in names)
            {
                people.Add(new Person(name.Trim()));
            }

            foreach (var line in preferences.Where(x => x.Trim().Length > 0))
            {
                var w    = line.Split('\t');
                var type = CandyTypeParser.Parse(w[0]);
                for (int i = 0; i < N; i++)
                {
                    var value = double.Parse(w[i + 1].Trim(), CultureInfo.InvariantCulture);
                    people[i].Preferences.Add(type, value);
                }
            }

            return(people);
        }
Example #2
0
        public static List <CandyType> Random(int amount, int seed = 1)
        {
            var rand  = new Random(seed);
            var enums = CandyTypeParser.GetAllTypes();
            var list  = Enumerable.Range(0, amount).Select(x => enums[rand.Next(0, enums.Count() - 1)]).ToList();

            return(new CandyBag(list));
        }
Example #3
0
        public List <Suggestion> GetSuggestions(List <Preference> preferences)
        {
            var suggestions = new List <Suggestion>();

            var types = CandyTypeParser.GetAllTypes();

            for (var i = 0; i < types.Count(); i++)
            {
                for (var j = i + 1; j < types.Count(); j++)
                {
                    if (preferences.Any(x => x.BestCount == 1 && x.WorstCount == 1 && (x.BestType == types[i] || x.WorstType == types[i]) && (x.BestType == types[j] || x.WorstType == types[j])))
                    {
                    }
                    else
                    {
                        suggestions.Add(new Suggestion()
                        {
                            TypeA  = types[i],
                            CountA = 1,
                            TypeB  = types[j],
                            CountB = 1,
                        });
                    }
                }
            }

            var usage      = preferences.Select(x => x.BestType).Concat(preferences.Select(x => x.WorstType)).ToList();
            var usageCount = types.ToDictionary(x => x, x => usage.Count(y => y == x));

            return(suggestions.OrderBy(x => usageCount[x.TypeA] + usageCount[x.TypeB]).Take(1).ToList());

            /*
             * var least = usageCount.OrderBy(x => x.Value).First().Key;
             * var compareUsage = preferences.Where(x => x.WorstType == least).Select(x => x.BestType).Concat(preferences.Where(x => x.BestType == least).Select(x =>  x.WorstType)).ToList();
             * var compoareUsageCount = types.ToDictionary(x => x, x => compareUsage.Count(y => y == x) + usageCount[x]);
             * var compareLeast = compoareUsageCount.OrderBy(x => x.Value).First(x => x.Key != least).Key;
             *
             * suggestions.Add(new Suggestion() {
             *  TypeA = least,
             *  CountA = 1,
             *  TypeB = compareLeast,
             *  CountB = 1,
             * });
             *
             * return suggestions;
             */
        }
Example #4
0
        public static List <CandyType> FromString(string contents)
        {
            var lines = contents.Replace("\r", "").Split("\n");
            var bag   = new List <CandyType>();

            foreach (var line in lines.Where(x => x.Trim().Length > 0))
            {
                var w     = line.Replace('\t', ' ').Trim().Split(' ');
                var type  = CandyTypeParser.Parse(w[0]);
                var value = w.Length > 1 ? int.Parse(w[1].Trim()) : 1;
                for (int i = 0; i < value; i++)
                {
                    bag.Add(type);
                }
            }
            return(bag);
        }
Example #5
0
        public static Person RandomPerson(string name, int seed = 0)
        {
            var rand   = new Random(seed);
            var person = new Person(name);

            var enums = CandyTypeParser.GetAllTypes();
            var scale = Enumerable.Range(1, 100).OrderBy(x => rand.Next()).ToList();
            var sum   = (double)scale.Take(enums.Count()).Sum();

            foreach (CandyType type in enums)
            {
                var value = scale.First();
                scale.Remove(value);
                person.Preferences.Add(type, value / sum);
            }

            return(person);
        }
Example #6
0
        public List <Rank> GetRank(List <Preference> preferences)
        {
            if (preferences == null)
            {
                preferences = new List <Preference>();
            }

            var types        = CandyTypeParser.GetAllTypes();
            var singles      = preferences.Where(x => x.BestCount == 1 && x.WorstCount == 1).OrderBy(x => Math.Min((int)x.BestType, (int)x.WorstType)).ToList();
            var missingTypes = types.Except(singles.Select(x => x.BestType).Concat(singles.Select(x => x.WorstType))).ToList();

            var rank = new List <Rank>();

            for (var i = 0; i < 1000; i++)
            {
                bool changed = false;
                foreach (var pref in singles.ToList())
                {
                    var best  = rank.FirstOrDefault(x => x.Type == pref.BestType);
                    var worst = rank.FirstOrDefault(x => x.Type == pref.WorstType);

                    if (best != null && worst != null)
                    {
                        var posBest  = rank.IndexOf(best);
                        var posWorst = rank.IndexOf(worst);
                        if (posWorst > posBest)
                        {
                            rank.Remove(worst);
                            rank.Insert(posBest, worst);
                        }
                    }

                    else if (best != null)
                    {
                        var pos = rank.IndexOf(best); // insert worst before best
                        rank.Insert(pos, new Rank()
                        {
                            Type = pref.WorstType,
                        });
                        changed = true;
                        singles.Remove(pref);
                    }
                    else if (worst != null)
                    {
                        var pos = rank.IndexOf(worst); // insert best before worst
                        if (pos == rank.Count() - 1)
                        {
                            rank.Add(new Rank()
                            {
                                Type = pref.BestType,
                            });
                        }
                        else
                        {
                            rank.Insert(pos + 1, new Rank()
                            {
                                Type = pref.BestType,
                            });
                        }
                        changed = true;
                        singles.Remove(pref);
                    }
                    else if (!rank.Any())
                    {
                        rank.Add(new Rank()
                        {
                            Type = pref.WorstType,
                        });
                        rank.Add(new Rank()
                        {
                            Type = pref.BestType,
                        });
                        changed = true;
                        singles.Remove(pref);
                    }
                    else
                    {
                        // happy
                    }

                    if (changed)
                    {
                        break;
                    }
                }

                if (!changed)
                {
                    break;
                }
            }

            return(rank);
        }
Example #7
0
 public List <CandyInfo> Info()
 {
     return(CandyTypeParser.GetAllTypes().Select(x => new CandyInfo(x)).ToList());
 }