Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace GroceryShop
- {
- class Program
- {
- static void Main(string[] args)
- {
- string input = Console.ReadLine();
- Regex pattern = new Regex(@"\b(?<![a-z]|[0-9])(?<product>[A-Z][a-z]+)[:](?<price>[0-9]+[.][0-9]{2})\b");
- var result = new Dictionary<string, double>();
- while (input != "bill")
- {
- if (pattern.IsMatch(input))
- {
- Match m = pattern.Match(input);
- string product = m.Groups["product"].Value;
- double price = double.Parse(m.Groups["price"].Value);
- if (!result.ContainsKey(product))
- {
- result.Add(product, price);
- }
- else
- {
- result[product] = price;
- }
- }
- input = Console.ReadLine();
- }
- var ordered = result.OrderByDescending(p => p.Value);
- foreach (var item in ordered)
- {
- Console.WriteLine($"{item.Key} costs {item.Value:f2}");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement