Advertisement
Guest User

List Operations

a guest
Oct 20th, 2019
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ListOfOperations
  6. {
  7.     class Program
  8.     {
  9.         public static void Main()
  10.         {
  11.             List<int> listOfIntegers =
  12.                 Console.ReadLine()
  13.                 .Split()
  14.                 .Select(int.Parse)
  15.                 .ToList();
  16.  
  17.             while (true)
  18.             {
  19.                 string lineInput = Console.ReadLine();
  20.                 if (lineInput == "End")
  21.                 {
  22.                     break;
  23.                 }
  24.  
  25.                 string[] commandInput = lineInput.Split();
  26.  
  27.                 switch (commandInput[0])
  28.                 {
  29.                     case "Add":
  30.                         listOfIntegers.Insert
  31.                             (listOfIntegers.Count,
  32.                             int.Parse(commandInput[1]));
  33.                         break;
  34.  
  35.                     case "Insert":
  36.                         listOfIntegers.Insert(
  37.                             int.Parse(commandInput[2]),
  38.                             int.Parse(commandInput[1]));
  39.                         break;
  40.  
  41.                     case "Remove":
  42.                         if (int.Parse(commandInput[1]) < 0 ||
  43.                             int.Parse(commandInput[1])>listOfIntegers.Count)
  44.                         {
  45.                             Console.WriteLine("Invalid index");
  46.                         }
  47.                         else
  48.                         {
  49.                             listOfIntegers.RemoveAt(int.Parse(commandInput[1]));
  50.                         }
  51.                         break;
  52.  
  53.                     case "Shift":
  54.                         if (commandInput[1] == "left")
  55.                         {
  56.                             for (int i = 0; i < int.Parse(commandInput[2]); i++)
  57.                             {
  58.                                 int firstIndexElement = listOfIntegers[0];
  59.                                 listOfIntegers.RemoveAt(0);
  60.                                 listOfIntegers.Insert
  61.                                     (listOfIntegers.Count,
  62.                                     firstIndexElement);
  63.                             }
  64.                         }
  65.                         if (commandInput[1] == "right")
  66.                         {
  67.                             for (int i = 0; i < int.Parse(commandInput[2]); i++)
  68.                             {
  69.                                int lastIndexElement = listOfIntegers[listOfIntegers.Count - 1];
  70.                                 listOfIntegers.RemoveAt(listOfIntegers.Count - 1);
  71.                                 listOfIntegers.Insert(0, lastIndexElement);
  72.                             }
  73.                         }
  74.                         break;
  75.  
  76.                     default:
  77.                         break;
  78.                 }
  79.             }
  80.             string result = string.Join(" ", listOfIntegers);
  81.             Console.WriteLine(result);
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement