JulianJulianov

10.RegularExpressionsMoreExercise-Winning Ticket

May 17th, 2020
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.10 KB | None | 0 0
  1.                                                 More Exercise: Regular Expressions
  2.  
  3. 01. Winning Ticket
  4. Lottery is exciting. What is not, is checking a million tickets for winnings only by hand. So, you are given the task to create a program which automatically checks if a ticket is a winner.
  5. You are given a collection of tickets separated by commas and spaces. You need to check every one of them if it has a winning combination of symbols.
  6. A valid ticket should have exactly 20 characters. The winning symbols are '@', '#', '$' and '^'. But in order for a ticket to be a winner the symbol should uninterruptedly repeat for at least 6 times in both the tickets left half and the tickets right half.
  7. For example, a valid winning ticket should be something like this:
  8. "Cash$$$$$$Ca$$$$$$sh"
  9. The left half "Cash$$$$$$" contains "$$$$$$", which is also contained in the tickets right half "Ca$$$$$$sh". A winning ticket should contain symbols repeating up to 10 times in both halves, which is considered a Jackpot (for example: "$$$$$$$$$$$$$$$$$$$$").
  10. Input
  11. The input will be read from the console. The input consists of a single line containing all tickets separated by commas and one or more white spaces in the format:
  12. "{ticket}, {ticket}, … {ticket}"
  13. Output
  14. Print the result for every ticket in the order of their appearance, each on a separate line in the format:
  15. • Invalid ticket - "invalid ticket"
  16. • No match - "ticket "{ticket}" - no match"
  17. • Match with length 6 to 9 - "ticket "{ticket}" - {match length}{match symbol}"
  18. • Match with length 10 - "ticket "{ticket}" - {match length}{match symbol} Jackpot!"
  19. Constrains
  20. • Number of tickets will be in range [0100]
  21. Examples
  22. Input                                                                          Output
  23. Cash$$$$$$Ca$$$$$$sh                                                           ticket "Cash$$$$$$Ca$$$$$$sh" - 6$
  24. $$$$$$$$$$$$$$$$$$$$, aabb  , th@@@@@@eemo@@@@@@ey                             ticket "$$$$$$$$$$$$$$$$$$$$" - 10$ Jackpot!
  25.                                                                                invalid ticket
  26.                                                                                ticket "th@@@@@@eemo@@@@@@ey" - 6@
  27. validticketnomatch:(                                                           ticket "validticketnomatch:(" - no match
  28. $$$$$$$$$$#^^$$$$$$$                                                           ticket "$$$$$$$$$$#^^$$$$$$$" - 7$
  29.  
  30. using System;
  31. using System.Linq;
  32. using System.Text.RegularExpressions;
  33.  
  34. public class Program
  35. {
  36.     public static void Main()
  37.     {
  38.          var allTicket = Console.ReadLine().Split(',').Select(x => x.Trim());
  39.  
  40.             foreach (var ticket in allTicket)
  41.             {
  42.                 if (ticket.Length != 20)
  43.                 {
  44.                     Console.WriteLine("invalid ticket");
  45.                     continue;
  46.                 }
  47.                 else
  48.                 {
  49.                     var leftHalf = ticket.Substring(0, 10);
  50.                     var rightHalf = ticket.Substring(10);
  51.                     var pattern = @"\^{6,10}|#{6,10}|@{6,10}|\${6,10}";
  52.                     var matchLeftHalf = Regex.Match(leftHalf, pattern);
  53.                     var matchRightHalf = Regex.Match(rightHalf, pattern);
  54.  
  55.                     if (matchLeftHalf.Success && matchRightHalf.Success && matchLeftHalf.Value[0] == matchRightHalf.Value[0])
  56.                     {
  57.                         var findMinValue = Math.Min(matchLeftHalf.Length, matchRightHalf.Length);
  58.                         if (findMinValue == 10)
  59.                         {
  60.                             Console.WriteLine($"ticket \"{ticket}\" - {findMinValue}{matchLeftHalf.Value[0]} Jackpot!");
  61.                         }
  62.                         else
  63.                         {
  64.                             Console.WriteLine($"ticket \"{ticket}\" - {findMinValue}{matchLeftHalf.Value[0]}");
  65.                         }
  66.                     }
  67.                     else
  68.                     {
  69.                         Console.WriteLine($"ticket \"{ticket}\" - no match");
  70.                     }
  71.                 }
  72.             }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment