×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: C#
Posted by: Alex Brah
Added: Apr 12, 2017 11:09 PM
Modified: Apr 13, 2017 10:44 PM
Views: 2328
Tags: no tags
  1.  
  2. /// <summary>
  3. /// The method "MostFrequentTriplet" should implement cancellation by token and return the most frequent triplet(s) from incoming words
  4. /// </summary>
  5. /// <param name="str">The string which contain only letters and commas </param>
  6. /// <param name="ct">Incoming cancellation token</param>
  7. /// <returns>All most frequent triplets and maximum frequency. For example, if str = "ala,bala,lylkuk,kukkuklyl,lyl", the method will return "lyl,kuk  3"</returns>
  8.  
  9. public string MostFrequentTriplet(string str, CancellationToken ct)
  10. {
  11.  
  12.     /// <summary>
  13.     /// The method will handle cancellation
  14.     /// </summary>
  15.  
  16.     if (ct.IsCancellationRequested)     //return string "Cenceled!" if cancellation was requested
  17.     {
  18.         try
  19.         {
  20.             ct.ThrowIfCancellationRequested();
  21.         }
  22.         catch (OperationCanceledException)
  23.         {
  24.             return "Canceled!";         //if method CancellationTokenSource.Cancel() was called ,the method will return "Canceled!"
  25.         }
  26.     }
  27.  
  28.     /// <summary>
  29.     /// The method will handle wrong "str" values
  30.     /// </summary>
  31.     /// <param name="match">A flag which defines do "str" belongs to the pattern @"^[A-Za-z,]+$" or not </param>
  32.  
  33.     Match match = Regex.Match(str, @"^[A-Za-z,]+$");
  34.     if (match.Success)
  35.     {
  36.         //if "str" belongs to the pattern @"^[A-Za-z,]+$, nothing happen
  37.     }
  38.     else
  39.         try
  40.         {
  41.             throw new RegexMatchTimeoutException();      //if "str" doesn't belong to the pattern @"^[A-Za-z,]+$, the method will handle RegexMatchTimeoutException
  42.         }
  43.         catch (RegexMatchTimeoutException)
  44.         {
  45.             return "Wrong 'str' argument, 'str' should contain only letters and commas";
  46.         }
  47.  
  48.  
  49.     /// <summary>
  50.     /// Find most frequent triplets in the array "words"
  51.     /// </summary>
  52.     /// <param name="words">Array of words from "str"</param>
  53.     /// <param name="triplet">Triplet is a combination of three adjoining letters</param>
  54.     /// <param name="mostFrequentTriplets">Dictionary of most frequent triplets (frequency is maximium and same for all these triplets)</param>
  55.     /// <param name="tripletFrequency">Frequency of a current triplet</param>
  56.     /// <param name="maxFrequency">Current maximum frequency</param>
  57.  
  58.     string[] words = str.Split(',');        //get array of words from "str"
  59.     string triplet;
  60.     Dictionary<string, int> mostFrequentTriplets = new Dictionary<string, int>();
  61.     int tripletFrequency, maxFrequency = 0;
  62.  
  63.     foreach (var word in words)         //will get triplets from incoming words
  64.     {
  65.         for (int i = 0; i < word.Length - 2; i++)        //will find most frequent triplets in the "str" by regular expression
  66.         {
  67.             triplet = word.Substring(i, 3);     //get a triplet
  68.             tripletFrequency = Regex.Matches(str, $"{triplet}").Count;      //get frequency of a triplet
  69.  
  70.             if (tripletFrequency >= maxFrequency)
  71.             {
  72.                 maxFrequency = tripletFrequency;     //find maximum frequency of triplets
  73.                 try
  74.                 {
  75.                     mostFrequentTriplets.Add(triplet, tripletFrequency);    //add triplets to dictionary
  76.                 }
  77.                 catch (ArgumentException)       //handle ArgumentException to ignore same triplets
  78.                 { continue; }
  79.             }
  80.         }
  81.     }
  82.  
  83.     foreach (var item in mostFrequentTriplets.Where(kvp => kvp.Value < maxFrequency).ToList())      //remove all not most frequent triplets(their frequency < maximum frequency)
  84.     {
  85.         mostFrequentTriplets.Remove(item.Key);      //remove unnecessary triplets from dictionary
  86.     }
  87.  
  88.     return string.Format($"{string.Join(",", mostFrequentTriplets.Keys.ToArray())}\t{maxFrequency}");      //return all most frequent triplets and maximum frequency. For example, if str = "ala,bala,lylkuk,kukkuklyl,lyl", the method will return "lyl,kuk  3"
  89. }
  90.