Advertisement
AlexTasev

05_ArrayManipulator

Jun 6th, 2018
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _05_ArrayManipulator
  6. {
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             List<int> inputList = Console.ReadLine().Split().Select(int.Parse).ToList();
  12.  
  13.             string input = Console.ReadLine();
  14.  
  15.             while (true)
  16.             {
  17.                 if (input == "print")
  18.                 {
  19.                     break;
  20.                 }
  21.  
  22.                 string[] commandsArr = input.Split();
  23.  
  24.                 string command = commandsArr[0];
  25.  
  26.                 if (command == "add")
  27.                 {
  28.                     int index = int.Parse(commandsArr[1]);
  29.                     int num = int.Parse(commandsArr[2]);
  30.                     inputList.Insert(index, num);
  31.                 }
  32.  
  33.                 else if (command == "addMany")
  34.                 {
  35.                     List<int> elementsToAdd = new List<int>();
  36.                     for (int i = 2; i < commandsArr.Length; i++)
  37.                     {
  38.                         elementsToAdd.Add(int.Parse(commandsArr[i]));
  39.                     }
  40.                     inputList.InsertRange(int.Parse(commandsArr[1]), elementsToAdd);
  41.                 }
  42.  
  43.                 else if (command == "contains")
  44.                 {
  45.                     int num = int.Parse(commandsArr[1]);
  46.  
  47.                     if (inputList.Contains(num))
  48.                     {
  49.                         for (int i = 0; i < inputList.Count; i++)
  50.                         {
  51.                             if (inputList[i] == num)
  52.                             {
  53.                                 Console.WriteLine(i);
  54.                             }
  55.                         }
  56.                     }
  57.                     else
  58.                     {
  59.                         Console.WriteLine(-1);
  60.                     }
  61.                 }
  62.  
  63.                 else if (command == "remove")
  64.                 {
  65.                     int index = int.Parse(commandsArr[1]);
  66.                     inputList.RemoveAt(index);
  67.                 }
  68.  
  69.                 else if (command == "shift")
  70.                 {
  71.                     int rotations = int.Parse(commandsArr[1]);
  72.                     for (int i = 0; i < rotations; i++)
  73.                     {
  74.                         ShiftElements(inputList);
  75.                     }
  76.                 }
  77.  
  78.                 else if (command == "shift pairs")
  79.                 {
  80.                     int sum = 0;
  81.  
  82.                     List<int> sumPair = new List<int>();
  83.  
  84.                     if (inputList.Count % 2 == 0)
  85.                     {
  86.                         for (int i = 0; i < inputList.Count; i += 2)
  87.                         {
  88.                             sum += inputList[i] + inputList[i + 1];
  89.                             sumPair.Add(sum);
  90.                         }
  91.                     }
  92.                     else
  93.                     {
  94.                         for (int i = 0; i < inputList.Count; i += 2)
  95.                         {
  96.                             sum += inputList[i] + inputList[i + 1];
  97.                             sumPair.Add(sum);
  98.                         }
  99.                         sumPair.Add(inputList[inputList.Count - 1]);
  100.                     }
  101.                 }
  102.  
  103.                 else if (command == "sumPairs")
  104.                 {
  105.                     for (int i = 0; i < inputList.Count - 1; i++)
  106.                     {
  107.                         inputList[i] += inputList[i + 1];
  108.                         inputList.RemoveAt(i + 1);
  109.                     }
  110.                 }
  111.  
  112.                 input = Console.ReadLine();
  113.             }
  114.  
  115.             Console.WriteLine("[" + string.Join(", ", inputList) + "]");
  116.         }
  117.  
  118.         static void ShiftElements(List<int> inputList)
  119.         {
  120.             int[] array = new int[inputList.Count];
  121.             array[array.Length - 1] = inputList[0];
  122.  
  123.             for (int i = array.Length - 2; i >= 0; i--)
  124.             {
  125.                 array[i] = inputList[i + 1];
  126.             }
  127.  
  128.             for (int i = 0; i < array.Length; i++)
  129.             {
  130.                 inputList[i] = array[i];
  131.             }
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement