elena1234

AdAstra-ExamPreparation

Dec 1st, 2020 (edited)
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.37 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6.  
  7. namespace AdAstra
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string input = Console.ReadLine();
  14.             var dictItemDate = new Dictionary<string, string>();
  15.             var dictItemCalories = new Dictionary<string, int>();
  16.             Regex regexForInput = new Regex(@"(#|\|)(?<item>[a-zA-Z\s]+)(\1)(?<date>[\d]{2}\/[\d]{2}\/[\d]{2})(\1)(?<calories>\d{1,5})(\1)");
  17.             MatchCollection matchesItemDateCalories = regexForInput.Matches(input);
  18.             int sumCalories = 0;
  19.             foreach (Match product in matchesItemDateCalories)
  20.             {
  21.                 int calories = int.Parse(product.Groups["calories"].Value);
  22.                 sumCalories += calories;
  23.             }
  24.  
  25.             int days = sumCalories / 2000;
  26.             Console.WriteLine($"You have food to last you for: {days} days!");
  27.  
  28.             foreach (Match product in matchesItemDateCalories)
  29.             {
  30.                 string item = product.Groups["item"].Value;
  31.                 string date = product.Groups["date"].Value;
  32.                 int calories = int.Parse(product.Groups["calories"].Value);
  33.                 Console.WriteLine($"Item: {item}, Best before: {date}, Nutrition: {calories}");
  34.             }
  35.         }
  36.     }
  37. }
Add Comment
Please, Sign In to add comment