LachezarNinkov

Untitled

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