Advertisement
martyz

Weather - OK

Oct 31st, 2017
3,148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Weather
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             string input = Console.ReadLine();
  15.             Regex p2 = new Regex(@"([A-Z]{2})(\d+\.\d+)([A-Za-z]+)(?=\|)");
  16.             var dict = new Dictionary<string, SortedDictionary<float, string>>();
  17.  
  18.             while (input != "end")
  19.             {
  20.                 Match m = p2.Match(input);
  21.                 if (m.Success)
  22.                 {
  23.                     string city = m.Groups[1].Value;
  24.                     float temperature = float.Parse(m.Groups[2].Value);
  25.                     string weather = m.Groups[3].Value;
  26.  
  27.                     if (dict.ContainsKey(city))
  28.                     {
  29.                         dict[city] = new SortedDictionary<float, string> { { temperature, weather } };
  30.                     }
  31.                     else
  32.                     {
  33.                         dict.Add(city, new SortedDictionary<float, string> { { temperature, weather } });
  34.                     }
  35.  
  36.                 }
  37.                 input = Console.ReadLine();
  38.             }
  39.  
  40.  
  41.             //(var item in dict.OrderBy(x => x.Value.temp))
  42.             dict = dict.OrderBy(x => x.Value.First().Key).ToDictionary(x => x.Key, x => x.Value);
  43.             foreach (var item in dict)
  44.             {
  45.                 Console.Write(item.Key + " => ");
  46.                 foreach (var i2 in item.Value)
  47.                 {
  48.                     Console.Write(i2.Key + " => " +i2.Value);
  49.                 }
  50.                 Console.WriteLine();
  51.             }
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement