Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ManipulateArray
- {
- class Program
- {
- static void Main(string[] args)
- {
- // Input
- string[] array = Console.ReadLine().Split();
- int commandCount = int.Parse(Console.ReadLine());
- for (int i = 0; i < commandCount; i++)
- {
- // Store command arguments, i.e. "Replace 2 Hello"
- string[] command = Console.ReadLine().Split();
- switch (command[0])
- {
- case "Reverse":
- array = ReverseArray(array);
- break;
- case "Distinct":
- array = DistinctArray(array);
- break;
- case "Replace":
- array = ReplaceArrayAt(array, int.Parse(command[1]), command[2]);
- break;
- default:
- break;
- }
- }
- // Output
- Console.WriteLine(string.Join(", ", array));
- }
- /// <summary>
- /// Reverses an array
- /// </summary>
- /// <param name="array"></param>
- /// <returns></returns>
- static string[] ReverseArray(string[] array)
- {
- for (int i = 0; i < array.Length / 2; i++)
- {
- // Index to switch with
- int to = array.Length - 1 - i;
- // Temporary save current value
- string temp = array[i];
- // Switch
- array[i] = array[to];
- array[to] = temp;
- }
- return array;
- }
- /// <summary>
- /// Removes duplicate entiries, leaving only first occurence
- /// </summary>
- /// <param name="array"></param>
- /// <returns></returns>
- static string[] DistinctArray(string[] array)
- {
- // Scan array
- for (int i = 0; i < array.Length; i++)
- {
- for (int j = i + 1; j < array.Length; j++)
- {
- // If found another occurence -> not unique, remove next occurences
- if (array[i] == array[j])
- {
- array = RemoveValueAt(array, j);
- j--;
- }
- }
- }
- return array;
- }
- /// <summary>
- /// Removes given value from a string array
- /// </summary>
- /// <param name="array"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- static string[] RemoveValueAt(string[] array, int atIndex)
- {
- // Shift array left
- for (int i = atIndex; i < array.Length - 1; i++)
- {
- array[i] = array[i + 1];
- }
- // Return shorter array
- string[] newArray = new string[array.Length - 1];
- for (int i = 0; i < newArray.Length; i++)
- {
- newArray[i] = array[i];
- }
- array = newArray;
- return array;
- }
- static string[] ReplaceArrayAt(string[] array, int index, string newValue)
- {
- array[index] = newValue;
- return array;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement