petaryankov00

03.PlantDiscovery

Dec 7th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5.  
  6. namespace PlantDiscovery
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             int n = int.Parse(Console.ReadLine());
  13.             Dictionary<string, Dictionary<string, double>> plants = new Dictionary<string, Dictionary<string,double>>();
  14.             Dictionary<string, List<int>> ratings = new Dictionary<string, List<int>>();
  15.             for (int i = 0; i < n; i++)
  16.             {
  17.                 string[] input = Console.ReadLine().Split("<->", StringSplitOptions.RemoveEmptyEntries);
  18.                 string name = input[0];
  19.                 int rarity = int.Parse(input[1]);
  20.                 if (!plants.ContainsKey(name))
  21.                 {
  22.                     plants.Add(name, new Dictionary<string, double>
  23.                    {
  24.                        {"rarity",0},
  25.                        {"averageRating",0}
  26.                    });                  
  27.                 }
  28.                 plants[name]["rarity"] += rarity;
  29.                
  30.             }
  31.             string command = Console.ReadLine();
  32.            
  33.             while (command != "Exhibition")
  34.             {
  35.                 string[] cmdArgs = command.Split(new string[] {" - ",": "},StringSplitOptions.RemoveEmptyEntries);
  36.                 string name = cmdArgs[1];
  37.                 if (cmdArgs[0] == "Rate")
  38.                 {
  39.                     int rating = int.Parse(cmdArgs[2]);
  40.                     if (plants.ContainsKey(name) && rating >=0)
  41.                     {
  42.                         if (ratings.ContainsKey(name))
  43.                         {
  44.                             ratings[name].Add(rating);
  45.                         }
  46.                         else
  47.                         {
  48.                             ratings.Add(name, new List<int> { rating });
  49.                         }                            
  50.                     }
  51.                     else
  52.                     {
  53.                         Console.WriteLine("error");
  54.                     }
  55.                 }
  56.                 else if (cmdArgs[0] == "Update")
  57.                 {
  58.                     int newRarity = int.Parse(cmdArgs[2]);
  59.                     if (plants.ContainsKey(name) && newRarity >=0)
  60.                     {
  61.                         plants[name]["rarity"] = newRarity;
  62.                     }
  63.                     else
  64.                     {
  65.                         Console.WriteLine("error");
  66.                     }
  67.                 }
  68.                 else if (cmdArgs[0] == "Reset")
  69.                 {
  70.                     if (plants.ContainsKey(name))
  71.                     {
  72.                         for (int i = 0; i < ratings[name].Count; i++)
  73.                         {
  74.                             ratings[name][i] = 0;
  75.                         }
  76.                     }
  77.                     else
  78.                     {
  79.                         Console.WriteLine("error");
  80.                     }
  81.                 }
  82.                 command = Console.ReadLine();
  83.             }          
  84.             foreach (var item in ratings)
  85.             {
  86.                 double sum = 0;
  87.                 for (int i = 0; i < item.Value.Count; i++)
  88.                 {
  89.                     sum += item.Value[i];
  90.                 }
  91.                 double average = sum / item.Value.Count;
  92.                 plants[item.Key]["averageRating"] = average;
  93.             }
  94.             Console.WriteLine("Plants for the exhibition:");
  95.             foreach (var currPlant in plants.OrderByDescending(x=>x.Value["rarity"]).ThenByDescending(a=>a.Value["averageRating"]))
  96.             {            
  97.                 Console.WriteLine($"- {currPlant.Key}; Rarity: {currPlant.Value["rarity"]}; Rating: {currPlant.Value["averageRating"]:f2}");
  98.             }
  99.         }
  100.     }
  101. }
  102.  
Add Comment
Please, Sign In to add comment