Advertisement
kolioi

Untitled

Jun 13th, 2017
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.17 KB | None | 0 0
  1. using System;
  2.  
  3. class Program
  4. {
  5.     static void Main()
  6.     {
  7.         string[] arr = Console.ReadLine().Split();
  8.         int n = int.Parse(Console.ReadLine());
  9.  
  10.         for (int i = 0; i < n; i++)
  11.         {
  12.             string[] commands = Console.ReadLine().Split();
  13.             switch (commands[0])
  14.             {
  15.                 case "Reverse":
  16.                     Reverse(arr);
  17.                     break;
  18.                 case "Distinct":
  19.                     arr = RemoveDuplicates(arr);
  20.                     break;
  21.                 case "Replace":
  22.                     Replace(arr, commands[1], commands[2]);
  23.                     break;
  24.                 default:
  25.                     // throw new Exception($"Invalid command '{commands[0]}'");
  26.             }
  27.         }
  28.  
  29.         Console.WriteLine(string.Join(", ", arr));
  30.     }
  31.  
  32.     private static string[] RemoveDuplicates(string[] arr)
  33.     {
  34.         int distinct = 0;
  35.         for (int i = 0; i < arr.Length; i++)
  36.         {
  37.             if (string.IsNullOrEmpty(arr[i]))
  38.                 continue;
  39.             else
  40.                 distinct++;
  41.  
  42.             for (int j = 0; j < arr.Length; j++)
  43.             {
  44.                 if (i == j)
  45.                     continue;
  46.                 if (arr[i].Equals(arr[j]))
  47.                     arr[j] = string.Empty; // arr[j] = null;
  48.             }
  49.         }
  50.  
  51.         if (arr.Length == distinct)
  52.             return arr;
  53.  
  54.         string[] distinctElements = new string[distinct];
  55.  
  56.         for (int i = 0, j = 0; i < arr.Length; i++)
  57.             if(!string.IsNullOrEmpty(arr[i]))
  58.                 distinctElements[j++] = arr[i];
  59.  
  60.         System.Diagnostics.Debug.Assert(distinctElements.Length == distinct);
  61.  
  62.         return distinctElements;
  63.     }
  64.  
  65.     private static void Replace(string[] arr, string stringIndex, string newString)
  66.     {
  67.         int index = int.Parse(stringIndex);
  68.         arr[index] =  newString;
  69.     }
  70.  
  71.     private static void Reverse(string[] arr)
  72.     {
  73.         for (int i = 0; i < arr.Length / 2; i++)
  74.         {
  75.             string temp = arr[i];
  76.             arr[i] = arr[arr.Length - i - 1];
  77.             arr[arr.Length - i - 1] = temp;
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement