Advertisement
alexbancheva

PlantDiscovery

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