silvana1303

plant discovery

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