bullit3189

Weather - RegexOld

Feb 26th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5. using System.Globalization;
  6.  
  7.  
  8. public class Program
  9. {
  10. public static void Main()
  11. {
  12. Dictionary<string,Dictionary<double,string>> cityTempWeather = new Dictionary<string,Dictionary<double,string>>();
  13.  
  14. while (true)
  15. {
  16. string input = Console.ReadLine();
  17.  
  18. if (input == "end")
  19. {
  20. break;
  21. }
  22.  
  23. string regex = @"((?<town>[A-Z]{2})(?<temp>\d+\.\d+)(?<weather>[A-Za-z]+))\|";
  24.  
  25. if (Regex.IsMatch(input,regex))
  26. {
  27. Match match = Regex.Match(input,regex);
  28.  
  29. string city = match.Groups["town"].Value;
  30. double temperature = double.Parse(match.Groups["temp"].Value);
  31. string typeOfWeather = match.Groups["weather"].Value;
  32.  
  33. if (char.IsLower(typeOfWeather[0]))
  34. {
  35. typeOfWeather = typeOfWeather.ToLower();
  36. }
  37. else
  38. {
  39. typeOfWeather = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(typeOfWeather);
  40. }
  41.  
  42. if (!cityTempWeather.ContainsKey(city))
  43. {
  44. cityTempWeather.Add(city, new Dictionary<double,string>());
  45. cityTempWeather[city].Add(temperature,typeOfWeather);
  46. }
  47. else
  48. {
  49. cityTempWeather[city]=new Dictionary<double,string>();
  50. cityTempWeather[city].Add(temperature,typeOfWeather);
  51. }
  52. }
  53. }
  54.  
  55. foreach (var kvp in cityTempWeather.OrderBy(x=>x.Value.Keys.Average()))
  56. {
  57. string city = kvp.Key;
  58.  
  59. foreach (var kvpValue in kvp.Value)
  60. {
  61. double temperature = kvpValue.Key;
  62. string type = kvpValue.Value;
  63.  
  64. Console.WriteLine("{0} => {1:f2} => {2}",city,temperature,type);
  65. }
  66. }
  67. }
  68. }
Add Comment
Please, Sign In to add comment