Advertisement
yahorrr

Untitled

Apr 24th, 2022
1,031
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ShiftArrayElements
  4. {
  5.     public static class EnumShifter
  6.     {
  7.         /// <summary>
  8.         /// Shifts elements in a <see cref="source"/> array using directions from <see cref="directions"/> array, one element shift per each direction array element.
  9.         /// </summary>
  10.         /// <param name="source">A source array.</param>
  11.         /// <param name="directions">An array with directions.</param>
  12.         /// <returns>An array with shifted elements.</returns>
  13.         /// <exception cref="ArgumentNullException">source array is null.</exception>
  14.         /// <exception cref="ArgumentNullException">directions array is null.</exception>
  15.         /// <exception cref="InvalidOperationException">direction array contains an element that is not <see cref="Direction.Left"/> or <see cref="Direction.Right"/>.</exception>
  16.         public static int[] Shift(int[] source, Direction[] directions)
  17.         {
  18.             // #1. Implement the method using "for" statements and indexers only (don't use Array.Copy method here).          
  19.             if (source is null)
  20.             {
  21.                 throw new ArgumentNullException(nameof(source));
  22.             }
  23.  
  24.             if (directions is null)
  25.             {
  26.                 throw new ArgumentNullException(nameof(directions));
  27.             }
  28.  
  29.             for (int i = 0; i < directions.Length; i++)
  30.             {
  31.                 switch (directions[i])
  32.                 {
  33.                     case Direction.Left:
  34.                         {
  35.                             int firstElemen = source[0];                      
  36.                             for (int k = 0; k < source.Length - 1; k++)
  37.                             {
  38.                                 source[k] = source[k + 1];
  39.                             }
  40.  
  41.                             source[^1] = firstElemen;
  42.  
  43.                             break;
  44.                         }
  45.  
  46.                     case Direction.Right:
  47.                         {
  48.                             int lastElement = source[^1];
  49.                             for (int k = source.Length - 1; k > 0; k--)
  50.                             {
  51.                                 source[k] = source[k - 1];
  52.                             }
  53.  
  54.                             source[0] = lastElement;
  55.  
  56.                             break;
  57.                         }
  58.  
  59.                     default:
  60.                         throw new InvalidOperationException($"Incorrect {directions} enum value.");
  61.                 }
  62.             }
  63.  
  64.             return source;
  65.         }
  66.     }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement