Advertisement
bacco

Array Manipulator

Jun 5th, 2018
97
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.  
  5.  
  6. namespace ArrayManipulator
  7. {
  8.     class MainClass
  9.     {
  10.         public static void Main(string[] args)
  11.         {
  12.             List<int> numbers = Console.ReadLine()
  13.                                        .Split(' ')
  14.                                        .Select(int.Parse)
  15.                                        .ToList();
  16.  
  17.             List<string> commands = Console.ReadLine()
  18.                                            .Split(' ')
  19.                                            .ToList();
  20.  
  21.             while (commands[0] != "print")
  22.             {
  23.                 if (commands[0] == "add")
  24.                 {
  25.                     int index   = int.Parse(commands[1]);
  26.                     int element = int.Parse(commands[2]);
  27.                     numbers.Insert(index, element);
  28.                 }
  29.                 else if (commands[0] == "addMany")
  30.                 {
  31.                     int index = int.Parse(commands[1]);
  32.                     numbers.InsertRange(index, commands
  33.                                                  .Skip(2)
  34.                                                  .Select(int.Parse));
  35.                 }
  36.                 else if (commands[0] == "contains")
  37.                 {
  38.                     int element = int.Parse(commands[1]);
  39.                     if (numbers.Contains(element))
  40.                     {
  41.                         Console.WriteLine(numbers.IndexOf(element));
  42.                     }
  43.                     else
  44.                     {
  45.                         Console.WriteLine(-1);
  46.                     }
  47.                 }
  48.                 else if (commands[0] == "remove")
  49.                 {
  50.                     int index = int.Parse(commands[1]);
  51.                     numbers.RemoveAt(index);
  52.                 }
  53.                 else if (commands[0] == "shift")
  54.                 {
  55.                     int position = int.Parse
  56.                                       (commands[1]) % numbers.Count;
  57.                     var helper = numbers.Skip(position).ToList();
  58.                     for (int i = 0; i < position ; i++)
  59.                     {
  60.                         helper.Add(numbers[i]);
  61.                     }
  62.                     numbers = helper;
  63.                 }
  64.                 else if (commands[0] == "sumPairs")
  65.                 {
  66.                     int lenght = numbers.Count / 2;
  67.  
  68.                     for (int i = 0; i < lenght; i++)
  69.                     {
  70.                         numbers[i] += numbers[i + 1];
  71.                         numbers.RemoveAt(i + 1);
  72.                     }
  73.                 }
  74.                 commands = Console.ReadLine()
  75.                                            .Split(' ')
  76.                                            .ToList();
  77.             }
  78.             Console.WriteLine("[" + string.Join(", ",numbers) + "]");
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement