Advertisement
Guest User

Untitled

a guest
Jan 7th, 2021
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace Plant_Discovery
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             int countOfLine = int.Parse(Console.ReadLine());
  13.             Dictionary<string, int> plantRarity = new Dictionary<string, int>();
  14.             Dictionary<string, List<double>> plantRate = new Dictionary<string, List<double>>();
  15.             for (int i = 0; i < countOfLine; i++)
  16.             {
  17.                 string line = Console.ReadLine();
  18.                 string[] lineSplit = line.Split("<->").ToArray();
  19.                 string name = lineSplit[0];
  20.                 int rarity = int.Parse(lineSplit[1]);
  21.                 if (!plantRarity.ContainsKey(name))
  22.                 {
  23.                     plantRarity.Add(name, rarity);
  24.                     plantRate.Add(name, new List<double>());
  25.                 }
  26.                 else
  27.                 {
  28.                     plantRarity[name] = rarity;
  29.                 }
  30.             }
  31.  
  32.             while (true)
  33.             {
  34.                 string line = Console.ReadLine();
  35.                 if (line == "Exhibition")
  36.                 {
  37.                     Console.WriteLine("Plants for the exhibition:");
  38.                     var sortedPlants = plantRarity
  39.                         .OrderByDescending(p => p.Value)
  40.                         .ThenByDescending(p => plantRate[p.Key].Count > 0
  41.                             ? plantRate[p.Key].Average()
  42.                             : 0.0);
  43.                    
  44.                     foreach (var (plantName, rarity) in sortedPlants)
  45.                     {
  46.                         double rating = plantRate[plantName].Count > 0
  47.                             ? plantRate[plantName].Average()
  48.                             : 0.0;
  49.                         Console.WriteLine(
  50.                             $"- {plantName}; Rarity: {rarity}; Rating: {rating:F2}");
  51.                     }
  52.  
  53.                     return;
  54.                 }
  55.  
  56.                 string[] lineSplit = Regex.Split(line, @"[:-]")
  57.                     .Select(s => s.Trim())
  58.                     .ToArray();
  59.                
  60.                 string name = lineSplit[1];
  61.                
  62.                 if (!plantRarity.ContainsKey(name))
  63.                 {
  64.                     Console.WriteLine("error");
  65.                     continue;
  66.                 }
  67.                
  68.                 switch (lineSplit[0])
  69.                 {
  70.                     case "Rate":
  71.                         double rate = double.Parse(lineSplit[2]);
  72.                         plantRate[name].Add(rate);
  73.                         break;
  74.                     case "Update":
  75.                         int newRarity = int.Parse(lineSplit[2]);
  76.                         plantRarity[name] = newRarity;
  77.                         break;
  78.                     case "Reset":
  79.                         plantRate[name].Clear();
  80.                         break;
  81.                 }
  82.             }
  83.         }
  84.     }
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement