JulianJulianov

12.RegularExpressionsMoreExercise - Post Office

May 23rd, 2020
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.39 KB | None | 0 0
  1. 03. Post Office
  2. You read a single line of ASCII symbols, and the message is somewhere inside it, you must find it.
  3.  The input consists of three parts separated with "|" like this:
  4. "{firstPart}|{secondPart}|{thirdPart}"
  5. Each word starts with capital letter and has a fixed length, you can find those in each different part of the input.
  6. The first part carries the capital letters for each word inside the message. You need to find those capital letters 1 or more from A to Z. The capital letters should be surrounded from the both sides with any of the following symbols – "#, $, %, *, &". And those symbols should match on the both sides. This means that $AOTP$ - is a valid pattern for the capital letters. $AKTP% - is invalid since the symbols do not match.
  7. The second part of the data contains the starting letter ASCII code and words length /between 120 charactes/, in the following format: "{asciiCode}:{length}". For example, "67:05" – means that '67' - ascii code equal to the capital letter "C", represents a word starting with "C" with following 5 characters: like "Carrot". The ascii code should be a capital letter equal to a letter from the first part. Word's length should be exactly 2 digits. Length less than 10 will always have a padding zero, you don't need to check that.
  8. The third part of the message are words separated by spaces. Those words have to start with Capital letter [A…Z] equal to the ascii code and have exactly the length for each capital letter you have found in the second part. Those words can contain any ASCII symbol without spaces.
  9. When you find valid word, you have to print it on a new line.
  10. Input / Constraints
  11. • On the first line – the text in form of three different parts separated by "|". There can be any ASCII character inside the input, except '|'.
  12. • Input will always be valid - you don’t need to check it
  13. • The input will always have three different parts, that will always be separated by "|".
  14. Output
  15. • Print all extracted words, each on a new line.
  16. • Allowed working time / memory: 100ms / 16MB
  17. Examples
  18. Input                                                   Output      Comment
  19. sdsGGasAOTPWEEEdas$AOTP$|a65:1.2s65:03d79:01ds84:02!    Argo        The capital letters are "AOTP" Then we look for the addition length
  20.  -80:07++ABs90:1.1|adsaArmyd Gara So La Arm Armyw21     Or          of the words for each capital letter. For A(65) -> it's 4.
  21. Argo O daOfa Or Ti Sar saTheww The Parahaos             The         For O(79) -> it's 2 For T(84) -> it's 3 For P(80) -> it's 8.
  22.                                                         Parahaos    Then we search in the last part for the words.First, start with
  23.                                                                     letter 'A' and we find "Argo". With letter 'O' we find  "Or".
  24.                                                                   With letter 'T' we find "The" and with letter 'P' we find "Parahaos".
  25.    
  26.  
  27.  
  28. Urgent"Message.TO$#POAML#|readData79:05:79:0!2reme80    Post           The first capital letters are "POAML"
  29. :03--23:11{79:05}tak{65:11ar}!77:!23--)77:05ACCSS76:    Office         Then we look for the addition length of the words for each
  30. 05ad|Remedy Por Ostream :Istream Post sOffices Office   Ankh-Morpork   capital letter. P(80) -> it's 4.   O(79) -> it's 6
  31. Of Ankh-Morpork MR.LIPWIG Mister Lipwig                Mister         A(65) -> it's 12  M(77) -> it's 6  L(76) -> it's 6.
  32.                                                         Lipwig         Then we search the last part for the words. First, start
  33.                                                                       with the letter 'P' and we find "Post". With letter 'O' we find
  34.                                                                      "Office". With letter 'A' we find "Ankh-Morpork". With letter 'M'
  35.                                                                       we find "Mister" and with letter 'L' we find "Lipwig".
  36. using System;
  37. using System.Collections.Generic;
  38. using System.Text.RegularExpressions;
  39.  
  40. public class Program
  41. {
  42.     public static void Main()
  43.     {
  44.            var findMessage = Console.ReadLine().Split('|');
  45.           var listCapitalLettersAndLength = new Dictionary<char, int>();
  46.           var listWord = new List<string>();
  47.           var counterParts = 0;
  48.           var capitalLetters = "";
  49.  
  50.            foreach (var part in findMessage)
  51.            {
  52.                counterParts++;
  53.                if (counterParts == 1)
  54.                {
  55.                    var pattern = @"(?<=(\$|#|%|&|\*))[A-Z]+(?=\1)";
  56.                     var matchCapitalLetters = Regex.Match(part, pattern);
  57.                     if (matchCapitalLetters.Success)
  58.                     {
  59.                         capitalLetters = matchCapitalLetters.Value;
  60.                         foreach (var item in capitalLetters)
  61.                         {
  62.                             listCapitalLettersAndLength.Add(item, 0);
  63.                         }
  64.                     }
  65.                 }
  66.                 else if (counterParts == 2)
  67.                 {
  68.                     var pattern = @"(?<=\D)(?<AsciiCode>\d{2}):(?<Length>\d{2})(?=\D)";
  69.                     var matchAsciiCodeAndLength = Regex.Matches(part, pattern);
  70.                     foreach (Match item in matchAsciiCodeAndLength)
  71.                     {
  72.                         var asciiCode = (char)int.Parse(item.Groups["AsciiCode"].Value);
  73.                         var length = int.Parse(item.Groups["Length"].Value);
  74.                         if (listCapitalLettersAndLength.ContainsKey(asciiCode) == true)
  75.                         {
  76.                             listCapitalLettersAndLength[asciiCode] = length + 1;
  77.                         }
  78.                     }
  79.                 }
  80.                 else
  81.                 {
  82.                     var words = part.Split();
  83.                     foreach (var letter in listCapitalLettersAndLength)
  84.                     {
  85.                         for (int i = 0; i < words.Length; i++)
  86.                         {
  87.                            
  88.                             if (words[i].Contains(letter.Key) && words[i].Length == letter.Value)
  89.                             {
  90.                                 listWord.Add(words[i]);
  91.                                 break;
  92.                             }
  93.                         }
  94.                     }
  95.                 }
  96.             }
  97.             Console.WriteLine(string.Join("\n", listWord));
  98.      }
  99. }
Add Comment
Please, Sign In to add comment