Advertisement
Guest User

Untitled

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