JulianJulianov

var regex = new Regex(pattern);

Jul 11th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.72 KB | None | 0 0
  1. Problem.1 Arriving in Kathmandu
  2. Your friend is a mountaineer and he needs your help. Your first task is to find him, so you went to Kathmandu and found some notes at his quarters.
  3.  
  4. Write a program that decrypts messages, which contain information about coordinates. You are looking for names of peaks in the Himalayas and their geohash coordinates. Keep reading lines until you receive the "Last note" message.
  5. Here is your cipher:
  6. • Name of the peak
  7. o   It is consisted of letters (upper and lower), numbers and some of the following characters between its letters – "!" "@" "#" "$" "?". Example for valid names:!@K?#2!#” (K2).
  8. • The length of the geohashcode
  9. o   Begins after the "=" (equals) sign and is consisted only of numbers.
  10. • The geohash code
  11. o   Begins after these symbols – "<<", may contain anything and the message always ends with it.
  12. Examples for valid input:
  13. "!Ma$$ka!lu!@=9<<ghtucjdhs" – all the components are there – name of the peek, length of the geohashcode and a geohashcode.
  14. "!@Eve?#rest!#=7<<vbnfhfg"
  15. Examples of invalid input:
  16. "anna@fg<<jhsd@bx!=4" – their order is wrong. The name should be first, the length after and the code last.
  17. "#n...s!n-<<tyuhgf4" – the length is missing and the name contains dots.
  18. "Nan$ga!Parbat=8<<gh2tn – the length of the geohash code doesn't match the given number.
  19. The geohash code you are looking for is with length exactly as much as the given length in the message and the information must be in the exact given order, otherwise it is considered invalid. If you find it, print the following message:
  20. "Coordinates found! {nameOfMountain} -> {geohashcode}"
  21. Otherwise print: “Nothing found!” after every invalid message.
  22. Input / Constraints
  23. • You will be receiving strings until you get the “Last note” message.
  24. Output
  25. • If you find the right coordinates, print: "Coordinates found! {nameOfMountain} -> {geohashcode}".
  26. • If the message is invalid, print: "Nothing found!".
  27. Examples
  28. Input                                        Output
  29. !@Ma?na?sl!u@=7<<tv58ycb4845                 Nothing found!
  30. E!ve?rest=.6<<tuvz26                         Nothing found!
  31. !K@2.,##$=4<<tvnd                            Nothing found!
  32. !Shiha@pan@gma##9<<tgfgegu67                 Nothing found!
  33. !###Anna@pur@na##=16<<tv5dekdz8x11ddkc       Coordinates found! Annapurna -> tv5dekdz8x11ddkc
  34. Last note  
  35.  
  36. Comments
  37. The first line is invalid, because the length – 7, doesn't match the length of the code.
  38. The second line is invalid, because the length should be consisted only of numbers.
  39. The third line is invalid, because the name contains symbols that are not allowed – ".", ",".
  40. The forth line is invalid, because the "=" sign before the length is missing.
  41. The fifth line is valid, so we print the appropriate message.
  42.  
  43. Ka?!#nch@@en@ju##nga@=3<<thfbghvn             Nothing found!
  44. =9Cho?@#Oyu<<thvb7ydht                        Nothing found!
  45. Nan??ga#Par!ba!t?=16<<twm03q2rx5hpmyr6        Coordinates found! NangaParbat -> twm03q2rx5hpmyr6
  46. Dhau??la#gi@ri?!#=3<<bvnfhrtiuy               Nothing found!
  47. Last note  
  48.  
  49.  
  50. using System;
  51. using System.Linq;
  52. using System.Text.RegularExpressions;
  53.  
  54. namespace FinalExam14April2019GroupI
  55. {
  56.    class FinalExam14April2019GroupI
  57.    {
  58.        static void Main(string[] args)
  59.        {
  60.            var encryptedMessages = "";
  61.            var patternValidMessages = @"^(?<NameOfThePeak>[A-Z0-9a-z!@#$?]+)=(?<LengthCode>[0-9]+)<<(?<Code>(.+))$";
  62.             var regex = new Regex(patternValidMessages);
  63.             while (!(encryptedMessages = Console.ReadLine()).Equals("Last note"))
  64.             {
  65.                 var matchValidMessages = regex.Match(encryptedMessages);
  66.                 if (matchValidMessages.Success)
  67.                 {
  68.                     var lengthCode = int.Parse(matchValidMessages.Groups["LengthCode"].Value);
  69.                     var code = matchValidMessages.Groups["Code"].Value;
  70.                     if (lengthCode == code.Length)
  71.                     {
  72.                         var searchName = matchValidMessages.Groups["NameOfThePeak"].Value.Where(x => char.IsLetterOrDigit(x)).ToList();
  73.                         var nameOfThePeak = "";
  74.                         foreach (var item in searchName)
  75.                         {
  76.                             nameOfThePeak += item;
  77.                         }
  78.                         Console.WriteLine($"Coordinates found! {nameOfThePeak} -> {code}");
  79.                     }
  80.                     else
  81.                     {
  82.                         Console.WriteLine("Nothing found!");  
  83.                     }
  84.                 }
  85.                 else
  86.                 {
  87.                     Console.WriteLine("Nothing found!");  
  88.                 }
  89.             }
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment