Advertisement
vaakata

ArrayManipulators_3._06._2016-100Points

Jun 3rd, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.60 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 ArrayManipulators_3._06._2016
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             List<long> inputNumbers = Console.ReadLine().Split(' ').Select(long.Parse).ToList();
  14.  
  15.             List<string> commands = new List<string>();
  16.             int indexCommand = 0;
  17.  
  18.             do
  19.             {
  20.                 commands.Add(Console.ReadLine());
  21.                 indexCommand++;
  22.  
  23.             } while (commands[indexCommand - 1] != "print");
  24.  
  25.             for (int i = 0; i < commands.Count; i++)
  26.             {
  27.                 string[] currentCommand = commands[i].Split(' ');
  28.  
  29.                 switch (currentCommand[0].ToLower())
  30.                 {
  31.                     case "add":
  32.                         inputNumbers.Insert(int.Parse(currentCommand[1]), long.Parse(currentCommand[2]));
  33.                         break;
  34.  
  35.                     case "addmany":
  36.                         AddMany(inputNumbers, currentCommand);
  37.                         break;
  38.  
  39.                     case "contains":
  40.                         FindingContainIndex(inputNumbers, currentCommand);
  41.                         break;
  42.  
  43.                     case "remove":
  44.                         inputNumbers.RemoveAt(int.Parse(currentCommand[1]));
  45.                         break;
  46.  
  47.                     case "shift":
  48.                         Shift(inputNumbers, currentCommand);
  49.                         break;
  50.  
  51.                     case "sumpairs":
  52.                         SumPairs(inputNumbers);
  53.                         break;
  54.  
  55.                     case "print":
  56.                         Console.Write('[');
  57.                         Console.Write(string.Join(", ", inputNumbers));
  58.                         Console.WriteLine(']');
  59.                         break;
  60.                 }
  61.             }
  62.         }
  63.  
  64.         public static void SumPairs(List<long> inputNumbers)
  65.         {
  66.             int countInputNumbers = inputNumbers.Count;
  67.             for (int iPairs = 0; iPairs < countInputNumbers / 2; iPairs++)
  68.             {
  69.                 inputNumbers[iPairs] += inputNumbers[iPairs + 1];
  70.                 inputNumbers.RemoveAt(iPairs + 1);
  71.             }
  72.         }
  73.  
  74.         public static void Shift(List<long> inputNumbers, string[] currentCommand)
  75.         {                    
  76.             int numberOfElements = int.Parse(currentCommand[1]);
  77.             for (int iShift = 0; iShift < numberOfElements; iShift++)
  78.             {
  79.                 inputNumbers.Add(inputNumbers[0]);
  80.                 inputNumbers.RemoveAt(0);
  81.             }            
  82.         }
  83.  
  84.         public static void FindingContainIndex(List<long> inputNumbers, string[] currentCommand)
  85.         {
  86.             long containNumber = long.Parse(currentCommand[1]);
  87.             Console.WriteLine(inputNumbers.IndexOf(containNumber));
  88.         }
  89.  
  90.         public static void AddMany(List<long> inputNumbers, string[] command)
  91.         {
  92.             long[] addElements = new long[command.Length - 2];
  93.             // saving add elements
  94.             for (int iSave = 0; iSave < command.Length - 2; iSave++)
  95.             {
  96.                 addElements[iSave] = long.Parse(command[iSave + 2]);
  97.             }
  98.             // inserting saved elements at particular index
  99.             for (int iInsert = addElements.Length - 1; iInsert >= 0; iInsert--)
  100.             {
  101.                 int index = int.Parse(command[1]);
  102.  
  103.                 inputNumbers.Insert(int.Parse(command[1]), addElements[iInsert]);
  104.             }
  105.         }
  106.  
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement