Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.93 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Linq;
  4.  
  5. namespace _01._Animal_Sanctuary
  6. {
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             var countLines = int.Parse(Console.ReadLine());
  12.             var weightOfAnimals = 0;
  13.  
  14.             for (int i = 0; i < countLines; i++)
  15.             {
  16.                 var input = Console.ReadLine();
  17.  
  18.                 var splittedInput = input
  19.                     .Split(";", StringSplitOptions.RemoveEmptyEntries);
  20.  
  21.                 var checkIsValid = true;
  22.  
  23.                 if (splittedInput.Length == 3)
  24.                 {
  25.                     var name = splittedInput[0];
  26.                     var kind = splittedInput[1];
  27.                     var country = splittedInput[2];
  28.  
  29.                     if (name[0] != 'n' || name[1] != ':')
  30.                     {
  31.                         checkIsValid = false;
  32.                     }
  33.  
  34.                     if (kind[0] != 't' || kind[1] != ':')
  35.                     {
  36.                         checkIsValid = false;
  37.                     }
  38.  
  39.                     if (country[0] != 'c' ||
  40.                         country[1] != '-' ||
  41.                         country[2] != '-')
  42.                     {
  43.                         checkIsValid = false;
  44.                     }
  45.  
  46.                     if (checkIsValid)
  47.                     {
  48.                         name = name.Substring(2);
  49.                         kind = kind.Substring(2);
  50.                         country = country.Substring(3);
  51.  
  52.                         var pattern = @"[A-Za-z]+";
  53.                         var nameAfterRegex = Regex.Matches(name, pattern);
  54.                         var kindAfterRegex = Regex.Matches(kind, pattern);
  55.                         var countryAfterRegex = Regex.Matches(country, pattern);
  56.  
  57.                         foreach (var item in nameAfterRegex)
  58.                         {
  59.                             Console.Write(item);
  60.                         }
  61.  
  62.                         Console.Write(" is a ");
  63.  
  64.                         foreach (var item in kindAfterRegex)
  65.                         {
  66.                             Console.Write(item);
  67.                         }
  68.  
  69.                         Console.Write(" from ");
  70.  
  71.                         foreach (var item in countryAfterRegex)
  72.                         {
  73.                             Console.Write(item);
  74.                         }
  75.  
  76.                         Console.WriteLine();
  77.  
  78.                         for (int j = 0; j < input.Length; j++)
  79.                         {
  80.                             var symbol = input[j];
  81.  
  82.                             if (char.IsDigit(symbol))
  83.                             {
  84.                                 weightOfAnimals += (symbol - 48);
  85.                             }
  86.                         }
  87.                     }
  88.                 }
  89.             }
  90.  
  91.             Console.WriteLine($"Total weight of animals: {weightOfAnimals}KG");
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement