Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _03._PlantDiscovery
- {
- class Program
- {
- static void Main(string[] args)
- {
- Dictionary<string, Plants> dictPlants = new Dictionary<string, Plants>();
- int n = int.Parse(Console.ReadLine());
- for (int i = 0; i < n; i++)
- {
- string[] info = Console.ReadLine().Split("<->",
- StringSplitOptions.RemoveEmptyEntries);
- string plant = info[0];
- int rarity = int.Parse(info[1]);
- Plants infoPlant = new Plants(rarity);
- if (!dictPlants.ContainsKey(plant))
- {
- dictPlants.Add(plant, infoPlant);
- }
- else
- {
- dictPlants[plant] = infoPlant;
- }
- }
- string command = Console.ReadLine();
- while (command != "Exhibition")
- {
- string[] currentCommand = command.Split(new char[] { ':', '-', ' ' },
- StringSplitOptions.RemoveEmptyEntries);
- string instruction = currentCommand[0];
- string plant = currentCommand[1];
- if (dictPlants.ContainsKey(plant))
- {
- if (instruction == "Rate")
- {
- double rating = double.Parse(currentCommand[2]);
- dictPlants[plant].Rating.Add(rating);
- }
- else if (instruction == "Update")
- {
- int newRarity = int.Parse(currentCommand[2]);
- dictPlants[plant].Rarity = newRarity;
- }
- else if (instruction== "Reset")
- {
- dictPlants[plant].Rating.Clear();
- }
- else
- {
- Console.WriteLine("error");
- }
- }
- else
- {
- Console.WriteLine("error");
- }
- command = Console.ReadLine();
- }
- foreach (var plant in dictPlants)
- {
- List<double> avrRating = plant.Value.Rating;
- int count = avrRating.Count;
- if (count == 0)
- {
- plant.Value.AvrgRating = 0;
- }
- else
- {
- plant.Value.AvrgRating = avrRating.Sum() / count;
- }
- }
- var orderedPlants = dictPlants
- .OrderByDescending(rarity => rarity.Value.Rarity)
- .ThenByDescending(avrg => avrg.Value.AvrgRating)
- .ToDictionary(k => k.Key, v => v.Value);
- Console.WriteLine("Plants for the exhibition:");
- foreach (var plant in orderedPlants)
- {
- string plantName = plant.Key;
- int rarity = plant.Value.Rarity;
- double avrRating = plant.Value.AvrgRating;
- Console.WriteLine($"- {plantName}; Rarity: {rarity}; Rating: {avrRating:f2}");
- }
- }
- public class Plants
- {
- public int Rarity { get; set; }
- public List<double> Rating { get; set; }
- public double AvrgRating { get; set; }
- public Plants(int rarity)
- {
- this.Rarity = rarity;
- this.Rating = new List<double>();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment