Programming Basics with C# - ноември 2020
Loading...
+ Нов въпрос
Axiomatik avatar Axiomatik 431 Точки
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
joandavis avatar joandavis 3 Точки

Nursing coursework writing service seekers have been on the rise lately since most learners need Nursing Healthcare Writing Services, healthcare assignment writing help and nursing essay writing services. 

-1