Advertisement
Guest User

Untitled

a guest
Feb 14th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.91 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace _04._List_Operations
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<int> numbers = Console.ReadLine()
  12.                 .Split()
  13.                 .Select(int.Parse)
  14.                 .ToList();
  15.  
  16.             string command = string.Empty;
  17.  
  18.             while ((command = Console.ReadLine()) != "End")
  19.             {
  20.                 string[] words = command.Split();
  21.                 string action = words[0];
  22.                 int num;
  23.  
  24.                 switch (action)
  25.                 {
  26.                     case "Add":
  27.                         num = int.Parse(words[1]);
  28.                         numbers.Add(num);
  29.                         break;
  30.                     case "Insert":
  31.                         num = int.Parse(words[1]);
  32.                         int indexNum = int.Parse(words[2]);
  33.  
  34.                         if (indexNum >= numbers.Count)
  35.                         {
  36.                             Console.WriteLine("Invalid index");
  37.                         }
  38.                         else
  39.                         {
  40.                             numbers.Insert(indexNum, num);
  41.                         }
  42.                         break;
  43.                     case "Remove":
  44.                         num = int.Parse(words[1]);
  45.  
  46.                         if (num >= numbers.Count)
  47.                         {
  48.                             Console.WriteLine("Invalid index");
  49.                         }
  50.                         else
  51.                         {
  52.                             numbers.RemoveAt(num);
  53.                         }
  54.                         break;
  55.                     case "Shift":
  56.                         int count = int.Parse(words[2]);
  57.                         ShiftTheList(numbers, count, words[1]);
  58.                         break;
  59.                 }
  60.             }
  61.  
  62.             Console.WriteLine(string.Join(" ", numbers));
  63.         }
  64.  
  65.         static void ShiftTheList(List<int> items, int count, string direction)
  66.         {
  67.             int nItem;
  68.  
  69.             if (direction == "right")
  70.             {
  71.                 for (int i = 0; i < count; i++)
  72.                 {
  73.                     nItem = items[items.Count - 1];     // get the last item of the list
  74.                     items.Remove(nItem);  // remove this item from the end  ...
  75.                     items.Insert(0, nItem);         // ... and insert this (last) item in front of the list
  76.                 }
  77.             }
  78.             else if (direction == "left")
  79.             {
  80.                 for (int i = 0; i < count; i++)
  81.                 {
  82.                     nItem = items[0];  //get the first element
  83.                     items.Insert(items.Count, nItem); //insert it at the back of the list
  84.                     items.Remove(items[0]);  //remove the element of the list
  85.                 }
  86.             }
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement