Advertisement
Guest User

Untitled

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