Advertisement
radostina92

PlantDiscovery

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