Guest User

Array Manipulator - all tests pass

a guest
Apr 10th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class ArrayManipulator
  6. {
  7.     public static void Main()
  8.     {
  9.         List<int> inputList = Console.ReadLine()
  10.             .Split(new char[] { ' ' }, StringSplitOptions
  11.             .RemoveEmptyEntries)
  12.             .Select(int.Parse)
  13.             .ToList();
  14.  
  15.         int counter = 0;
  16.         while (true)
  17.         {
  18.             counter++;
  19.             if (counter > 1000)
  20.             {
  21.                 GC.Collect();
  22.                 counter = 0;
  23.             }
  24.             List<string> inputCommands = Console.ReadLine()
  25.             .Split(new char[] { ' ' }, StringSplitOptions
  26.             .RemoveEmptyEntries)
  27.             .ToList();
  28.  
  29.             string command = inputCommands[0];
  30.  
  31.             List<int> parameters = new List<int>();
  32.             for (int i = 1; i < inputCommands.Count; i++)
  33.             {
  34.                 parameters.Add(int.Parse(inputCommands[i]));
  35.             }
  36.  
  37.  
  38.             if (command == "print")
  39.             {
  40.                 break;
  41.             }
  42.  
  43.             switch (command)
  44.             {
  45.                 case "add":
  46.                     inputList.Insert(parameters[0], parameters[1]);
  47.                     break;
  48.  
  49.                 case "addMany":
  50.                     int position = parameters[0];
  51.                     parameters.RemoveAt(0);
  52.                     inputList.InsertRange(position, parameters);
  53.                     break;
  54.  
  55.                 case "contains":
  56.                     Console.WriteLine(inputList.IndexOf(parameters[0]));
  57.                     break;
  58.  
  59.                 case "remove":
  60.                     inputList.RemoveAt(parameters[0]);
  61.                     break;
  62.  
  63.                 case "shift":
  64.                     int shiftAmmount = parameters[0] % inputList.Count;
  65.                     for (int i = 0; i < shiftAmmount; i++)
  66.                     {
  67.                         int temp = inputList[0];
  68.                         inputList.RemoveAt(0);
  69.                         inputList.Add(temp);
  70.                     }
  71.                     break;
  72.  
  73.                 case "sumPairs":
  74.                     for (int i = 0; i < inputList.Count-1; i++)
  75.                     {
  76.                         inputList[i] = inputList[i] + inputList[i + 1];
  77.                         inputList.RemoveAt(i + 1);
  78.                     }
  79.                     break;
  80.  
  81.                 default:
  82.                     break;
  83.  
  84.             }
  85.         }
  86.  
  87.         Console.WriteLine($"[{ string.Join(", ", inputList)}]");
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment