Advertisement
yanass

Arriving at Kathmandu

Jul 30th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace Arriving_at_Katmandu
  7. {
  8. class Program
  9. {
  10. static void Main()
  11. {
  12. //solved with a little regex and many if-s
  13. while (true)
  14. {
  15. string enterInfo = Console.ReadLine();
  16.  
  17. if (enterInfo == "Last note")
  18. {
  19. break;
  20. }
  21.  
  22. else
  23. {
  24. bool isCorrect = true;
  25.  
  26. //enter the data for the peaks, split to get the mountain
  27.  
  28. string[] data = enterInfo.Split("=").ToArray();
  29.  
  30. //should be of two parts - mountain and the rest of the data
  31. if (data.Length == 2)
  32. {
  33. //get the mountain name to check if it is correct
  34. string namePeak = data[0];
  35.  
  36. //get pattern
  37. Regex RgxUrl = new Regex("^[a-zA-Z0-9!@#$?]+$");
  38.  
  39. //check if the string matches the pattern, returns true/false
  40. bool isPeak = RgxUrl.IsMatch(namePeak);
  41.  
  42. if (isPeak == true)
  43. {
  44. //we have it
  45. string mountain = GetName(namePeak);
  46.  
  47. //split to get the length and the name of the hashcode
  48. string[] lengthNameHash = data[1].Split("<<").ToArray();
  49.  
  50. //should be of length 2
  51. if (lengthNameHash.Length == 2)
  52. {
  53. //the first element must be an integer
  54. int length;
  55.  
  56. //check if it is integer
  57. bool isNum = int.TryParse(lengthNameHash[0], out length);
  58.  
  59.  
  60. if (isNum)
  61. {
  62. //get the hashcode name
  63. string geohash = lengthNameHash[1];
  64.  
  65. //check if the name is with the stated length
  66. if (geohash.Length == length)
  67. Console.WriteLine($"Coordinates found! {mountain} -> {geohash}");
  68. else
  69. isCorrect = false;
  70. }
  71. else
  72. isCorrect = false;
  73. }
  74. }
  75. else
  76. isCorrect = false;
  77.  
  78. }
  79. else
  80. isCorrect = false;
  81.  
  82. if (isCorrect == false)
  83. {
  84. Console.WriteLine("Nothing found!");
  85. }
  86. }
  87. }
  88. }
  89.  
  90. static string GetName(string name)
  91. {
  92. string newName = "";
  93.  
  94. for (int i = 0; i < name.Length; i++)
  95. {
  96. if (name[i] >= 'A' && name[i] <= 'Z'
  97. || name[i] >= 'a' && name[i] <= 'z'
  98. || name[i] >= '0' && name[i] <= '9')
  99. {
  100. newName += name[i];
  101. }
  102. }
  103.  
  104. return newName;
  105. }
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement