vstoyanov

Array Manipulator

Oct 17th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.86 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 _05.Array_Manipulator
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             List<int> inputNumbers = Console.ReadLine().Split(' ').Select(int.Parse).ToList();
  14.  
  15.             string command = Console.ReadLine();
  16.  
  17.             List<int> sumNumbers = new List<int>();
  18.  
  19.             while (command != "print")
  20.             {
  21.                 string[] inputCommands = command.Split(' ');
  22.  
  23.                 if (inputCommands[0] == "add")
  24.                 {
  25.                     int index = Int32.Parse(inputCommands[1]);
  26.                     int element = Int32.Parse(inputCommands[2]);
  27.  
  28.                     inputNumbers.Insert(index, element);
  29.  
  30.                 } else if (inputCommands[0] == "addMany")
  31.  
  32.  
  33.                 {
  34.                     int index = Int32.Parse(inputCommands[1]);
  35.  
  36.                     for (int i = 2; i < inputCommands.Length; i++)
  37.                     {
  38.                         int element = Int32.Parse(inputCommands[i]);
  39.  
  40.                        
  41.                             inputNumbers.Insert(index+i-2, element);
  42.                        
  43.                        
  44.                            
  45.  
  46.                        
  47.  
  48.  
  49.  
  50.                     }
  51.  
  52.                 } else if (inputCommands[0] == "contains")
  53.                 {
  54.                     int element = Int32.Parse(inputCommands[1]);
  55.  
  56.                     if (inputNumbers.Contains(element))
  57.                     {
  58.                         Console.WriteLine(inputNumbers.IndexOf(element));
  59.                     } else
  60.                     {
  61.                         Console.WriteLine("-1");
  62.                     }
  63.                 } else if (inputCommands[0] == "remove")
  64.                 {
  65.                     int index = Int32.Parse(inputCommands[1]);
  66.  
  67.                     inputNumbers.RemoveAt(index);
  68.  
  69.  
  70.                 } else if (inputCommands[0] == "shift")
  71.                 {
  72.                     int position = Int32.Parse(inputCommands[1]);
  73.  
  74.  
  75.                     for (int i = 0; i < position; i++)
  76.                     {
  77.                         int first = inputNumbers[i];
  78.                         inputNumbers.RemoveAt(i);
  79.                         inputNumbers.Add(first);
  80.  
  81.  
  82.                     }
  83.  
  84.                 } else if(inputCommands[0]== "sumPairs")
  85.                 {
  86.                     for (int i = 0; i < inputNumbers.Count-1; i++)
  87.                     {
  88.                         inputNumbers[i] = inputNumbers[i] + inputNumbers[i + 1];
  89.                         inputNumbers.RemoveAt(i + 1);
  90.  
  91.                     }
  92.  
  93.  
  94.  
  95.                 }
  96.  
  97.                 command = Console.ReadLine();
  98.             }
  99.  
  100.  
  101.             Console.WriteLine("["+ string.Join(", ", inputNumbers)+"]");
  102.            
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment