Advertisement
sivancheva

CubicMessage

Sep 19th, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace _04_CubicMessages
  9. {
  10. class CubicMessages
  11. {
  12. static void Main(string[] args)
  13. {
  14.  
  15. var input = Console.ReadLine();
  16.  
  17. while (input != "Over!")
  18. {
  19. int n = int.Parse(Console.ReadLine());
  20. var pattern = new Regex($@"^(?<index1>\d+)(?<word>[a-zA-Z]{{{n}}})(?<index2>[^a-zA-Z]*)$");
  21.  
  22. var match = pattern.Match(input);
  23.  
  24.  
  25. if (!match.Success)
  26. {
  27. input = Console.ReadLine(); //???
  28. continue;
  29. }
  30.  
  31. var word = match.Groups["word"].Value;
  32.  
  33. //if (word.Length != n)
  34. //{
  35. // input = Console.ReadLine(); //???
  36. // continue;
  37. //}
  38. var index1 = match.Groups["index1"].Value.ToCharArray().Select(x => x - 48).ToList();
  39. var index2 = match.Groups["index2"].Value; //string
  40. bool allCharactersInStringAreDigits = index2.Any(char.IsDigit); // proverjavame dali ima digiti
  41.  
  42. var indexes = new List<int>(index1);
  43.  
  44. if (allCharactersInStringAreDigits) // ako ima digit vzimame samo tjah i gi dobavjame v list indexes
  45. {
  46. var index2Array = index2.ToCharArray().Where(x => char.IsDigit(x)).Select(x => x - 48).ToList();
  47. indexes.AddRange(index2Array);
  48. }
  49.  
  50. var result = new List<char>();
  51.  
  52. for (int i = 0; i < indexes.Count; i++)
  53. {
  54. if (indexes[i] > word.Length - 1)
  55. {
  56. char space = ' ';
  57. result.Add(space);
  58. }
  59. else
  60. {
  61. result.Add(word[indexes[i]]);
  62. }
  63.  
  64. }
  65. Console.WriteLine($"{word} == " + String.Join("", result));
  66.  
  67. input = Console.ReadLine();
  68. }
  69.  
  70. }
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement