Advertisement
elena1234

PlantDiscovery-calculate Average() with ? :

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