public static int GetNecessaryWeightToBalance(ProgramModel topmostUnbalanced) { var desiredWeight = GetModeWeightOfChildren(topmostUnbalanced.Parent); var diff = desiredWeight - topmostUnbalanced.WeightIncludingAllChildren; return(topmostUnbalanced.Weight + diff); }
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)); }
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"); }
private static int GetModeWeightOfChildren(ProgramModel program) { return(program.Children.GroupBy(p => p.WeightIncludingAllChildren). OrderByDescending(g => g.Count()). Select(g => g.Key).FirstOrDefault()); }