Loading...

Във форума е въведено ограничение, което позволява на потребителите единствено да разглеждат публикуваните въпроси.

Axiomatik avatar Axiomatik 2422 Точки
Best Answer

Some tricky parts were included in this exam exercise. For this solution, the crucial point was to first go trough the rarities/sortedPlants collection and to re-assign two new values to the ratings collection for each plant : the rarity number of the plant and the calculated average of the plant (line 77 - 92). In addition, while the code is still processing the “Exhibition” loop, an additional validation had to be included for non-existing plant-names (line 42 – 46).

Best,

New code(100%) :

using System;
using System.Linq;
using System.Collections.Generic;

namespace Plant_Discovery
{
    class Program
    {
        static void Main(string[] args)
        {
            int numberOfPlants = int.Parse(Console.ReadLine());
            Dictionary<string, int> rarities = new Dictionary<string, int>();
            Dictionary<string, List<double>> ratings = new Dictionary<string, List<double>>();

            for (int i = 0; i < numberOfPlants; i++)
            {
                string[] informtionForPlants = Console.ReadLine()
                    .Split("<->");
                string plant = informtionForPlants[0];
                int rarity = int.Parse(informtionForPlants[1]);

                if (rarities.ContainsKey(plant))
                {
                    rarities[plant] = rarity;
                }
                else
                {
                    rarities.Add(plant, rarity);
                    ratings.Add(plant, new List<double>());
                }
            }

            string command;

            while ((command = Console.ReadLine()) != "Exhibition")
            {
                string[] receivedCommand = command.Split(": ");
                string firstPartOfCommand = receivedCommand[0];
                string[] secondPartOfCommand = receivedCommand[1].Split(" - ");
                string plant = secondPartOfCommand[0];

                if (!ratings.ContainsKey(plant))
                {
                    Console.WriteLine("error");
                    continue;
                }

                if (firstPartOfCommand == "Rate")
                {
                    int rating = int.Parse(secondPartOfCommand[1]);

                    ratings[plant].Add(rating);
                }
                else if (firstPartOfCommand == "Update")
                {
                    int rarity = int.Parse(secondPartOfCommand[1]);

                    rarities[plant] = rarity;
                }
                else if (firstPartOfCommand == "Reset")
                {
                    //ratings[plant].RemoveAll(x => x > 0);
                    ratings[plant].Clear();
                }
                else
                {
                    Console.WriteLine("error");
                }
            }

            Console.WriteLine("Plants for the exhibition:");

            var sortedPlants = rarities
                .OrderByDescending(p => p.Value)
                .ToDictionary(key => key.Key, value => value.Value);

            foreach (var (plant, rarity) in sortedPlants)
            {
                if (ratings.ContainsKey(plant))
                {
                    double average = 0;

                    if (ratings[plant].Any())
                    {
                        average = ratings[plant].Average();
                    }

                    ratings[plant].Clear();
                    ratings[plant].Add(rarity);
                    ratings[plant].Add(average);
                }
            }

            foreach (var (plant, data) in ratings.OrderByDescending(p => p.Value[0]).ThenByDescending(p => p.Value[1]))
            {

                Console.WriteLine($"- {plant}; Rarity: {data[0]}; Rating: {data[1]:f2}");

                /*foreach (var item in ratings.OrderByDescending(x => x.Value.Sum() / x.Value.Count))
                {
 
                }*/
            }
        }
    }
}

0
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.