NastySwipy

Progr Fundamentals - Lists - 05. Array Manipulator

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