gospod1978

List-Ex/List Operations

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