Advertisement
elena1234

WinningTicket*-matchAllCollectionAndMatchOneSymbol

Nov 26th, 2020 (edited)
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.11 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4.  
  5. namespace TextAndRegex
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string[] ticketsCollection =
  12.                 Console.ReadLine().Split(new string[] { ",", " " }, StringSplitOptions.RemoveEmptyEntries);
  13.             Regex regexForWinningTicket = new Regex(@"([@]{6,10})|([#]{6,10})|([$]{6,10})|([\^]{6,10})");
  14.  
  15.             foreach (string ticket in ticketsCollection)
  16.             {
  17.                 if (ticket.Length != 20)
  18.                 {
  19.                     Console.WriteLine("invalid ticket");
  20.                 }
  21.  
  22.                 else if (ticket.Length == 20) //only 20 is valid ticket length
  23.                 {
  24.                     string leftSide = ticket.Substring(0, 10); //left side
  25.                     string rightSide = ticket.Substring(10, 10); //right side
  26.                     MatchCollection matchLeftSide = regexForWinningTicket.Matches(leftSide); // match left side to check is it valid
  27.                     MatchCollection matchRightSide = regexForWinningTicket.Matches(rightSide); //match right side to check is it valid
  28.  
  29.                     if (regexForWinningTicket.IsMatch(leftSide) && regexForWinningTicket.IsMatch(rightSide))
  30.                     {
  31.                         if (matchLeftSide[0].Value.Contains(matchRightSide[0].Value[0]) == false)//if the all left collection contains the first symbol from the right collection==false
  32.                         {
  33.                             Console.WriteLine($"ticket \"{ticket}\" - no match");
  34.                         }
  35.  
  36.                         else if (matchLeftSide[0].Value[0]==matchRightSide[0].Value[0])//if the first symbol from the left collection is equal to the first symbol from the right collection
  37.                         {
  38.                             char theSameSymbol = matchRightSide[0].Value[0];
  39.                             int validTheRepeatingLengthInBothSide = Math.Min(
  40.                                 matchLeftSide[0].Value.Length,
  41.                                 matchRightSide[0].Value.Length);
  42.                             if ((validTheRepeatingLengthInBothSide >= 6 && validTheRepeatingLengthInBothSide <= 9))
  43.                             {
  44.                                 Console.WriteLine(
  45.                                     $"ticket \"{ticket}\" - {validTheRepeatingLengthInBothSide}{theSameSymbol}");
  46.                             }
  47.  
  48.                             else if ((validTheRepeatingLengthInBothSide == 10))
  49.                             {
  50.                                 Console.WriteLine(
  51.                                 $"ticket \"{ticket}\" - {validTheRepeatingLengthInBothSide}{theSameSymbol} Jackpot!");
  52.                             }
  53.                         }
  54.                     }
  55.  
  56.                     else if (regexForWinningTicket.IsMatch(leftSide) == false ||
  57.                              regexForWinningTicket.IsMatch(rightSide) == false)
  58.                     {
  59.                         Console.WriteLine($"ticket \"{ticket}\" - no match");
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.     }
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement