GerganaTsirkova

Weather

Feb 24th, 2018
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace Weather
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             string input = Console.ReadLine();
  13.             SortedDictionary<string, Dictionary<double, string>> weatherData = new SortedDictionary<string, Dictionary<double, string>>();
  14.  
  15.             while(input != "end")
  16.             {
  17.                 string pattern = @"(?<city>[A-Z]{1}[A-Z]{1})(?<temp>[0-9]+\.([0-9]+))(?<weather>[A-Za-z]+)\|";
  18.                 var matches = Regex.Match(input, pattern);
  19.                 if(matches.Success==true)
  20.                 {
  21.                     var city = matches.Groups["city"].Value;
  22.                     var temperature = double.Parse(matches.Groups["temp"].Value);
  23.                     var weather = matches.Groups["weather"].Value;
  24.                     weather = weather.TrimEnd('|');
  25.                     var insideDict = new Dictionary<double, string>();
  26.                     insideDict.Add(temperature, weather);
  27.                     if(!weatherData.ContainsKey(city))
  28.                     {
  29.                         weatherData.Add(city, insideDict);
  30.                     }
  31.                     else
  32.                     {
  33.                         weatherData[city].Clear();
  34.                         weatherData[city].Add(temperature,weather);
  35.                     }
  36.                    
  37.                 }
  38.                 input = Console.ReadLine();
  39.             }
  40.             foreach (var data in weatherData)
  41.             {
  42.                 Console.Write($"{data.Key}");
  43.                 foreach (var city in data.Value)
  44.                 {
  45.                     Console.WriteLine(" => {0:f2} => {1}",city.Key,city.Value);
  46.                 }
  47.             }
  48.  
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment