Guest User

Array Manipulator

a guest
Apr 28th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5.  
  6. class Program
  7. {
  8.     static void Main(string[] args)
  9.     {
  10.  
  11.         List<int> nums = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToList();
  12.         while (true)
  13.         {
  14.             var input = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
  15.             if (input[0] == "print")
  16.             {
  17.                 break;
  18.             }
  19.             switch (input[0])
  20.             {
  21.                 case "add":
  22.                     nums.Insert(int.Parse(input[1]), int.Parse(input[2]));
  23.                     break;
  24.                 case "addMany":
  25.                     for (int i = input.Count - 1; i >= 2; i--)
  26.                     {
  27.                         nums.Insert(int.Parse(input[1]), int.Parse(input[i]));
  28.                     }
  29.                     break;
  30.                 case "contains":
  31.                     int contains = int.Parse(input[1]);
  32.                     Console.WriteLine(nums.IndexOf(contains));
  33.                     break;
  34.                 case "remove":
  35.                     nums.RemoveAt(int.Parse(input[1]));
  36.                     break;
  37.                 case "shift":
  38.                     int positions = int.Parse(input[1]);
  39.                     for (int i = 0; i < positions; i++)
  40.                     {
  41.                         int lastElement = nums[0];
  42.                         for (int j = 0; j < nums.Count - 1; j++)
  43.                         {
  44.                             nums[j] = nums[j + 1];
  45.                         }
  46.                         nums[nums.Count - 1] = lastElement;
  47.                     }
  48.                     break;
  49.                 case "sumPairs":
  50.                     List<int> sumPairs = new List<int>();
  51.                     for (int i = 0; i < nums.Count - 1; i += 2)
  52.                     {
  53.                         sumPairs.Add(nums[i] + nums[i + 1]);
  54.                     }
  55.                     if (nums.Count % 2 == 1)
  56.                     {
  57.                         sumPairs.Add(nums[nums.Count - 1]);
  58.                     }
  59.                     nums = sumPairs;
  60.                     break;
  61.             }
  62.         }
  63.         Console.WriteLine("["+String.Join(", ", nums)+"]");
  64.     }
  65. }
Add Comment
Please, Sign In to add comment