private static void Main()
        {
            int number = int.Parse(Console.ReadLine());

            Dictionary <string, List <Dragon> > typeAndDragons = new Dictionary <string, List <Dragon> >();

            for (int i = 0; i < number; i++)
            {
                string input = Console.ReadLine();

                string[] inputArgs = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                Dragon dragon = SetDragonStats(inputArgs);

                AddDragonToDictionary(typeAndDragons, dragon);
            }

            PrintResult(typeAndDragons);
        }
        private static void AddDragonToDictionary(Dictionary <string, List <Dragon> > typeAndDragons, Dragon dragon)
        {
            string type = dragon.Type;
            string name = dragon.Name;

            if (!typeAndDragons.ContainsKey(type))
            {
                typeAndDragons[type] = new List <Dragon>();
            }

            if (typeAndDragons[type].Any(dr => dr.Name == name))
            {
                int indexOfDragonToOverwrite = typeAndDragons[type].FindIndex(dr => dr.Name == name);
                typeAndDragons[type][indexOfDragonToOverwrite] = dragon;
            }
            else
            {
                typeAndDragons[type].Add(dragon);
            }
        }
        //With this method we are creating a Dragon and keep his info the in array of strings "commandArgs"
        public static Dragon CreateDragon(string[] commandArgs)
        {
            Dragon dragon = new Dragon();

            dragon.Type = commandArgs[0];
            dragon.Name = commandArgs[1];
            //If a stat is missing you should assign it default values.
            //Default values are as follows: health 250, damage 45, and armor 10.
            //Missing stat will be marked by null.
            if (commandArgs[2] != "null")
            {
                dragon.Damage = int.Parse(commandArgs[2]);
            }
            else
            {
                dragon.Damage = 45;
            }

            if (commandArgs[3] != "null")
            {
                dragon.Health = int.Parse(commandArgs[3]);
            }

            else
            {
                dragon.Health = 250;
            }

            if (commandArgs[4] != "null")
            {
                dragon.Armor = int.Parse(commandArgs[4]);
            }
            else
            {
                dragon.Armor = 10;
            }

            return(dragon);
        }
        static void Main()
        {
            var dragonsData = new Dictionary <string, SortedDictionary <string, Dragon> >();

            int n = int.Parse(Console.ReadLine());

            for (int i = 0; i < n; i++)
            {
                string[] tokens = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                string type  = tokens[0];
                string name  = tokens[1];
                int    dmg   = 0;
                int    hp    = 0;
                int    armor = 0;

                if (tokens[2] == "null")
                {
                    dmg = 45;
                }
                else
                {
                    dmg = int.Parse(tokens[2]);
                }

                if (tokens[3] == "null")
                {
                    hp = 250;
                }
                else
                {
                    hp = int.Parse(tokens[3]);
                }

                if (tokens[4] == "null")
                {
                    armor = 10;
                }
                else
                {
                    armor = int.Parse(tokens[4]);
                }

                Dragon dragon = new Dragon(dmg, hp, armor);

                if (!dragonsData.ContainsKey(type))
                {
                    dragonsData.Add(type, new SortedDictionary <string, Dragon>());
                }

                if (!dragonsData[type].ContainsKey(name))
                {
                    dragonsData[type].Add(name, dragon);
                }
                dragonsData[type][name] = dragon;
            }

            foreach (var data in dragonsData)
            {
                string type = data.Key;
                SortedDictionary <string, Dragon> statsData = data.Value;

                double averageDmg    = statsData.Values.Average(d => d.Damage);
                double averageHealth = statsData.Values.Average(h => h.Health);
                double averageArmor  = statsData.Values.Average(a => a.Armor);

                Console.WriteLine($"{type}::({averageDmg:f2}/{averageHealth:f2}/{averageArmor:f2})");

                foreach (KeyValuePair <string, Dragon> stats in statsData)
                {
                    string name   = stats.Key;
                    Dragon dragon = stats.Value;

                    Console.WriteLine($"-{name} -> damage: {dragon.Damage}, health: {dragon.Health}, armor: {dragon.Armor}");
                }
            }
        }
Exemple #5
0
        static void Main(string[] args)
        {
            Dictionary <string, Dictionary <string, List <int> > > dragons =
                new Dictionary <string, Dictionary <string, List <int> > >();

            int           n        = int.Parse(Console.ReadLine());
            List <Dragon> dragonss = new List <Dragon>();

            for (int i = 0; i < n; i++)
            {
                string[] commandArgs = Console.ReadLine().Split(new char[] { ' ' },
                                                                StringSplitOptions.RemoveEmptyEntries);

                Dragon dragon = CreateDragon(commandArgs);
                if (!dragonss.Any(d => d.Name == dragon.Name && d.Type == dragon.Type))
                {
                    dragonss.Add(CreateDragon(commandArgs));
                }

                else
                {
                    var rem = dragonss.FirstOrDefault(d => d.Name == dragon.Name && d.Type == dragon.Type);
                    if (commandArgs[2] != "null")
                    {
                        rem.Damage = int.Parse(commandArgs[2]);
                    }
                    else
                    {
                        rem.Damage = 45;
                    }
                    if (commandArgs[3] != "null")
                    {
                        rem.Health = int.Parse(commandArgs[3]);
                    }
                    else
                    {
                        rem.Health = 250;
                    }
                    if (commandArgs[4] != "null")
                    {
                        rem.Armor = int.Parse(commandArgs[4]);
                    }
                    else
                    {
                        rem.Armor = 10;
                    }
                }
            }

            foreach (string typee in dragonss.Select(d => d.Type).Distinct().ToList())
            {
                var averageDamage = dragonss
                                    .Where(d => d.Type == typee)
                                    .Average(d => d.Damage);
                var averageHealth = dragonss
                                    .Where(d => d.Type == typee)
                                    .Average(d => d.Health);
                var averageArmor = dragonss
                                   .Where(d => d.Type == typee)
                                   .Average(d => d.Armor);

                Console.WriteLine($"{typee}::({averageDamage:F2}/{averageHealth:F2}/{averageArmor:F2})");

                foreach (var dragon in dragonss.Where(d => d.Type == typee).OrderBy(e => e.Name))
                {
                    Console.WriteLine($"-{dragon.Name} -> damage: {dragon.Damage}, health: {dragon.Health}, armor: {dragon.Armor}");
                }
            }
        }
        static void Main()
        {
            int n = int.Parse(Console.ReadLine());

            List <Dragon> dragons = new List <Dragon>();

            for (int i = 0; i < n; i++)
            {
                string[] commandArgs = Console.ReadLine()
                                       .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                Dragon dragon = CreateDragon(commandArgs);

                if (!dragons.Any(d => d.Name == dragon.Name && d.Type == dragon.Type))
                {   //We create the dragon with his stats :Type, Name, Damage, Health and Armor,
                    //and saving it directly to the List "dragon
                    dragons.Add(CreateDragon(commandArgs));
                }
                //If the same dragon is added a second time, the new stats should overwrite the previous ones.
                //Two dragons are considered equal if they match by both name and type.
                else
                {
                    var reminder = dragons.FirstOrDefault(d => d.Name == dragon.Name && d.Type == dragon.Type);

                    if (commandArgs[2] != "null")
                    {
                        reminder.Damage = int.Parse(commandArgs[2]);
                    }
                    else
                    {
                        reminder.Damage = 45;
                    }

                    if (commandArgs[3] != "null")
                    {
                        reminder.Health = int.Parse(commandArgs[3]);
                    }

                    else
                    {
                        reminder.Health = 250;
                    }

                    if (commandArgs[4] != "null")
                    {
                        reminder.Armor = int.Parse(commandArgs[4]);
                    }
                    else
                    {
                        reminder.Armor = 10;
                    }
                }
            }
            //Categorize dragons by their type.
            foreach (string dragonType in dragons.Select(d => d.Type).Distinct().ToList())
            {
                var averageDamage = dragons.Where(d => d.Type == dragonType).Average(d => d.Damage);
                var averageHealth = dragons.Where(d => d.Type == dragonType).Average(d => d.Health);
                var averageArmor  = dragons.Where(d => d.Type == dragonType).Average(d => d.Armor);
                //For each type print the average damage, health and armor of the dragons.
                Console.WriteLine($"{dragonType}::({averageDamage:f2}/{averageHealth:f2}/{averageArmor:f2})");

                foreach (var dragon in dragons.Where(d => d.Type == dragonType).OrderBy(x => x.Name))
                {
                    Console.WriteLine($"-{dragon.Name} -> damage: {dragon.Damage}, health: {dragon.Health}, armor: {dragon.Armor}");
                }
            }
        }