Advertisement
yahorrr

Untitled

Apr 24th, 2022
1,049
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ShiftArrayElements
  4. {
  5.     public static class Shifter
  6.     {
  7.         /// <summary>
  8.         /// Shifts elements in a <see cref="source"/> array using <see cref="iterations"/> array for getting directions and iterations (see README.md for detailed instructions).
  9.         /// </summary>
  10.         /// <param name="source">A source array.</param>
  11.         /// <param name="iterations">An array with iterations.</param>
  12.         /// <returns>An array with shifted elements.</returns>
  13.         /// <exception cref="ArgumentNullException">source array is null.</exception>
  14.         /// <exception cref="ArgumentNullException">iterations array is null.</exception>
  15.         public static int[] Shift(int[] source, int[] iterations)
  16.         {
  17.             // #2. Implement the method using "for" statements and Array.Copy method.
  18.             if (source is null)
  19.             {
  20.                 throw new ArgumentNullException(nameof(source));
  21.             }
  22.  
  23.             if (iterations is null)
  24.             {
  25.                 throw new ArgumentNullException(nameof(iterations));
  26.             }
  27.  
  28.             int sumEven = 0;
  29.             int sumOdd = 0;
  30.             for (int i = 0; i < iterations.Length; i += 2)
  31.             {
  32.                 sumEven += iterations[i];
  33.             }
  34.  
  35.             for (int i = 1; i < iterations.Length; i += 2)
  36.             {
  37.                 sumOdd += iterations[i];
  38.             }
  39.  
  40.             int iteration;
  41.  
  42.             if (sumEven > sumOdd)
  43.             {
  44.                 iteration = sumEven - sumOdd;
  45.                 ShiftLeft(source, iteration);
  46.             }
  47.             else if (sumEven == sumOdd)
  48.             {
  49.                 return source;
  50.             }
  51.             else
  52.             {
  53.                 iteration = sumOdd - sumEven;
  54.                 ShiftRight(source, iteration);
  55.             }
  56.  
  57.             return source;
  58.         }
  59.  
  60.         private static void ShiftLeft(int[] array, int iterations = 1)
  61.         {
  62.             for (int i = 0; i < iterations; i++)
  63.             {
  64.                 int firstElemen = array[0];
  65.                 for (int k = 0; k < array.Length - 1; k++)
  66.                 {
  67.                     array[k] = array[k + 1];
  68.                 }
  69.  
  70.                 array[^1] = firstElemen;
  71.             }
  72.         }
  73.  
  74.         private static void ShiftRight(int[] array, int iterations = 1)
  75.         {
  76.             for (int i = 0; i < iterations; i++)
  77.             {
  78.                 int lastElement = array[^1];
  79.                 for (int k = array.Length - 1; k > 0; k--)
  80.                 {
  81.                     array[k] = array[k - 1];
  82.                 }
  83.  
  84.                 array[0] = lastElement;
  85.             }
  86.         }
  87.     }
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement