예제 #1
0
        public static int GetNecessaryWeightToBalance(ProgramModel topmostUnbalanced)
        {
            var desiredWeight = GetModeWeightOfChildren(topmostUnbalanced.Parent);

            var diff = desiredWeight - topmostUnbalanced.WeightIncludingAllChildren;

            return(topmostUnbalanced.Weight + diff);
        }
예제 #2
0
        private static ProgramModel FindUnbalancedChild(ProgramModel program)
        {
            var modeWeight = GetModeWeightOfChildren(program);

            if (program.Children.All(p => p.WeightIncludingAllChildren == modeWeight))
            {
                return(null);
            }

            return(program.Children.First(p => p.WeightIncludingAllChildren != modeWeight));
        }
예제 #3
0
        public static ProgramModel ParseLine(string line)
        {
            var match = _regex.Match(line);

            if (match.Success)
            {
                var programModel = new ProgramModel
                {
                    Name   = match.Groups["name"].Value,
                    Weight = int.Parse(match.Groups["weight"].Value)
                };
                var childrenNames = match.Groups["children"].Value;
                if (!string.IsNullOrWhiteSpace(childrenNames))
                {
                    programModel.ChildrenNames.AddRange(childrenNames.Split(',').Select(s => s.Trim()));
                }
                return(programModel);
            }
            throw new ArgumentException("Could not parse the line as a ProgramModel");
        }
예제 #4
0
 private static int GetModeWeightOfChildren(ProgramModel program)
 {
     return(program.Children.GroupBy(p => p.WeightIncludingAllChildren).
            OrderByDescending(g => g.Count()).
            Select(g => g.Key).FirstOrDefault());
 }