Advertisement
veronikaaa86

Array Manipulator C#

Feb 13th, 2018
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 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 ConsoleApp6
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             //List<int> nums = Console.ReadLine().Split(' ').Select(int.Parse).ToList();
  14.             string[] input = Console.ReadLine().Split(' ');
  15.             List<int> nums = new List<int>();
  16.  
  17.             foreach (var item in input)
  18.             {
  19.                 nums.Add(int.Parse(item));
  20.             }
  21.  
  22.             List<string> command = new List<string>(3);
  23.  
  24.             do
  25.             {
  26.  
  27.                 command = Console.ReadLine().Split(' ').ToList();
  28.                 switch (command[0])
  29.                 {
  30.                     case "add":
  31.                         {
  32.                             nums.Insert(int.Parse(command[1]), int.Parse(command[2]));
  33.                             break;
  34.                         }
  35.  
  36.                     case "addMany":
  37.                         {
  38.                             nums.InsertRange(int.Parse(command[1]), command.Skip(2).Select(int.Parse).ToArray());
  39.                             break;
  40.                         }
  41.                     case "contains":
  42.                         {
  43.  
  44.                             Console.WriteLine(nums.IndexOf(int.Parse(command[1])));
  45.                             break;
  46.                         }
  47.  
  48.                     case "remove":
  49.                         {
  50.                             nums.RemoveAt(int.Parse(command[1]));
  51.                             break;
  52.                         }
  53.  
  54.                     case "shift":
  55.                         {
  56.                             int shiftCycles = int.Parse(command[1]);
  57.                             while (shiftCycles > 0)
  58.                             {
  59.                                 int first = nums[0];
  60.                                 nums.RemoveAt(0);
  61.                                 nums.Add(first);
  62.                                 shiftCycles--;
  63.  
  64.                             }
  65.                             break;
  66.                         }
  67.  
  68.                     case "sumPairs":
  69.                         {
  70.                             for (int i = 0; i < nums.Count - 1; i++)
  71.                             {
  72.                                 nums[i] = nums[i] + nums[i + 1];
  73.                                 nums.RemoveAt(i + 1);
  74.                             }
  75.                             break;
  76.                         }
  77.                 }
  78.  
  79.             }
  80.             while (command[0] != "print");
  81.  
  82.             Console.WriteLine($"[{string.Join(", ", nums)}]");
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement