Advertisement
Guest User

Animal Sanctuary

a guest
Nov 22nd, 2019
1,533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.94 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. namespace _01._Animal_Sanctuary
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             string pattern = @"n:(?<name>.+?);t:(?<kind>.+?);c--(?<country>[A-Za-z\s]+)";
  11.  
  12.             int totalWeight = 0;
  13.  
  14.             int numberOflines = int.Parse(Console.ReadLine());
  15.  
  16.             for (int i = 0; i < numberOflines; i++)
  17.             {
  18.                 string input = Console.ReadLine();
  19.  
  20.                 Match match = Regex.Match(input, pattern);
  21.  
  22.                 if (match.Success)
  23.                 {
  24.                     string encryptedName = match.Groups["name"].Value;
  25.                     string name = DecryptString(encryptedName);
  26.  
  27.                     string encryptedKind = match.Groups["kind"].Value;
  28.                     string kind = DecryptString(encryptedKind);
  29.  
  30.                     string country = match.Groups["country"].Value;
  31.                     totalWeight += SumInputDigits($"{encryptedName}{encryptedKind}");
  32.  
  33.  
  34.                     Console.WriteLine($"{name} is a {kind} from {country}");
  35.                 }
  36.             }
  37.  
  38.             Console.WriteLine($"Total weight of animals: {totalWeight}KG");
  39.         }
  40.  
  41.         private static int SumInputDigits(string match)
  42.         {
  43.             int sum = 0;
  44.  
  45.             for (int i = 0; i < match.Length; i++)
  46.             {
  47.                 if (char.IsDigit(match[i]))
  48.                 {
  49.                     sum += match[i] - 48;
  50.                 }
  51.             }
  52.  
  53.             return sum;
  54.         }
  55.  
  56.         private static string DecryptString(string encryptedName)
  57.         {
  58.             string name = string.Empty;
  59.  
  60.             for (int i = 0; i < encryptedName.Length; i++)
  61.             {
  62.                 if (char.IsLetter(encryptedName[i]))
  63.                 {
  64.                     name += encryptedName[i];
  65.                 }
  66.             }
  67.  
  68.             return name;
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement