Advertisement
yahorrr

Untitled

Jun 2nd, 2022
730
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ShuffleCharacters
  4. {
  5.     public static class StringExtension
  6.     {
  7.         /// <summary>
  8.         /// Shuffles characters in source string according some rule.
  9.         /// </summary>
  10.         /// <param name="source">The source string.</param>
  11.         /// <param name="count">The count of iterations.</param>
  12.         /// <returns>Result string.</returns>
  13.         /// <exception cref="ArgumentException">Source string is null or empty or white spaces.</exception>
  14.         /// <exception cref="ArgumentException">Count of iterations is less than 0.</exception>
  15.         public static string ShuffleChars(string source, int count)
  16.         {
  17.             if (string.IsNullOrWhiteSpace(source))
  18.             {
  19.                 throw new ArgumentException("Source string is null or empty or white spaces.", nameof(source));
  20.             }
  21.  
  22.             if (count < 0)
  23.             {
  24.                 throw new ArgumentException("Count of iterations is less than 0", nameof(count));
  25.             }
  26.  
  27.             bool SequenceEqual(char[] a, char[] b)
  28.             {
  29.                 for (int i = 0; i < a.Length; i++)
  30.                 {
  31.                     if (a[i] != b[i])
  32.                     {
  33.                         return false;
  34.                     }
  35.                 }
  36.  
  37.                 return true;
  38.             }
  39.  
  40.             char[] result = source.ToCharArray();
  41.             char[] array = new char[source.Length];
  42.             char[] start = new char[source.Length];
  43.             Array.Copy(result, start, result.Length);
  44.             int t = 0;
  45.             int middle = (source.Length / 2) + (source.Length % 2);
  46.  
  47.             for (int i = 0; i < count; i++)
  48.             {
  49.                 (array, result) = (result, array);
  50.  
  51.                 for (int j = 0; j < middle; j++)
  52.                 {
  53.                     result[j] = array[j * 2];
  54.                 }
  55.  
  56.                 for (int j = middle, k = 1; j < source.Length; j++, k += 2)
  57.                 {
  58.                     result[j] = array[k];
  59.                 }
  60.  
  61.                 t++;
  62.  
  63.                 if (SequenceEqual(result, start))
  64.                 {
  65.                     while (count > i + t)
  66.                     {
  67.                         count -= t;
  68.                     }
  69.                 }
  70.             }
  71.  
  72.             return new string(result);
  73.         }
  74.     }
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement