Advertisement
Aborigenius

GroceryShop83/100

Aug 27th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 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 GroceryShop
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             string input = Console.ReadLine();
  15.             Regex pattern = new Regex(@"\b(?<![a-z]|[0-9])(?<product>[A-Z][a-z]+)[:](?<price>[0-9]+[.][0-9]{2})\b");
  16.                                      
  17.             var result = new Dictionary<string, double>();
  18.             while (input != "bill")
  19.             {
  20.                 if (pattern.IsMatch(input))
  21.                 {
  22.                     Match m = pattern.Match(input);
  23.                     string product = m.Groups["product"].Value;
  24.                     double price = double.Parse(m.Groups["price"].Value);
  25.  
  26.                     if (!result.ContainsKey(product))
  27.                     {
  28.                         result.Add(product, price);
  29.                     }
  30.                     else
  31.                     {
  32.                         result[product] = price;
  33.                     }
  34.                 }
  35.  
  36.                 input = Console.ReadLine();
  37.             }
  38.             var ordered = result.OrderByDescending(p => p.Value);
  39.             foreach (var item in ordered)
  40.             {
  41.                 Console.WriteLine($"{item.Key} costs {item.Value:f2}");
  42.             }
  43.          
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement