Advertisement
bobypenev

02. Manipulate Array

Jun 5th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ManipulateArray
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             // Input
  14.             string[] array = Console.ReadLine().Split();
  15.             int commandCount = int.Parse(Console.ReadLine());
  16.  
  17.             for (int i = 0; i < commandCount; i++)
  18.             {
  19.                 // Store command arguments, i.e. "Replace 2 Hello"
  20.                 string[] command = Console.ReadLine().Split();
  21.                 switch (command[0])
  22.                 {
  23.                     case "Reverse":
  24.                         array = ReverseArray(array);
  25.                         break;
  26.                     case "Distinct":
  27.                         array = DistinctArray(array);
  28.                         break;
  29.                     case "Replace":
  30.                         array = ReplaceArrayAt(array, int.Parse(command[1]), command[2]);
  31.                         break;
  32.                     default:
  33.                         break;
  34.                 }
  35.  
  36.             }
  37.  
  38.             // Output
  39.             Console.WriteLine(string.Join(", ", array));
  40.         }
  41.  
  42.         /// <summary>
  43.         /// Reverses an array
  44.         /// </summary>
  45.         /// <param name="array"></param>
  46.         /// <returns></returns>
  47.         static string[] ReverseArray(string[] array)
  48.         {
  49.             for (int i = 0; i < array.Length / 2; i++)
  50.             {                
  51.                 // Index to switch with
  52.                 int to = array.Length - 1 - i;
  53.  
  54.                 // Temporary save current value
  55.                 string temp = array[i];
  56.                
  57.                 // Switch
  58.                 array[i] = array[to];
  59.                 array[to] = temp;
  60.             }
  61.  
  62.             return array;
  63.         }
  64.  
  65.         /// <summary>
  66.         /// Removes duplicate entiries, leaving only first occurence
  67.         /// </summary>
  68.         /// <param name="array"></param>
  69.         /// <returns></returns>
  70.         static string[] DistinctArray(string[] array)
  71.         {
  72.             // Scan array
  73.             for (int i = 0; i < array.Length; i++)
  74.             {
  75.                 for (int j = i + 1; j < array.Length; j++)
  76.                 {
  77.                     // If found another occurence -> not unique, remove next occurences
  78.                     if (array[i] == array[j])
  79.                     {
  80.                         array = RemoveValueAt(array, j);
  81.                         j--;
  82.                     }
  83.                 }
  84.             }
  85.  
  86.             return array;
  87.         }
  88.  
  89.         /// <summary>
  90.         /// Removes given value from a string array
  91.         /// </summary>
  92.         /// <param name="array"></param>
  93.         /// <param name="value"></param>
  94.         /// <returns></returns>
  95.         static string[] RemoveValueAt(string[] array, int atIndex)
  96.         {
  97.             // Shift array left
  98.             for (int i = atIndex; i < array.Length - 1; i++)
  99.             {
  100.                 array[i] = array[i + 1];
  101.             }
  102.  
  103.             // Return shorter array
  104.             string[] newArray = new string[array.Length - 1];
  105.             for (int i = 0; i < newArray.Length; i++)
  106.             {
  107.                 newArray[i] = array[i];
  108.             }
  109.  
  110.             array = newArray;
  111.  
  112.             return array;
  113.         }
  114.  
  115.         static string[] ReplaceArrayAt(string[] array, int index, string newValue)
  116.         {
  117.             array[index] = newValue;
  118.             return array;
  119.         }
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement