Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace List_Operations
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.  
  12.  
  13.             List<int> numbers = Console.ReadLine().Split().Select(int.Parse).ToList();
  14.             string[] command = Console.ReadLine().Split().ToArray();
  15.  
  16.  
  17.            
  18.             while (command[0] != "End")
  19.             {
  20.  
  21.                 if (command[0] == "Add")
  22.                 {
  23.                     int cmdArgs = int.Parse(command[1]);
  24.                     Add(numbers, cmdArgs);
  25.                 }
  26.  
  27.  
  28.                 else if (command[0] == "Insert")
  29.  
  30.                 {
  31.                     int cmdArgs = int.Parse(command[1]);
  32.                     int cmdArgs2 = int.Parse(command[2]);
  33.                     Insert(numbers, cmdArgs, cmdArgs2);
  34.  
  35.                 }
  36.  
  37.                 else if (command[0] == "Remove")
  38.                 {
  39.  
  40.                     int cmdArgs = int.Parse(command[1]);
  41.                    
  42.                     Remove(numbers,cmdArgs );
  43.                 }
  44.  
  45.  
  46.                 else if (command[0] == "Shift" && command[1] == "left")
  47.                 {
  48.                     int cmdArgs2 = int.Parse(command[2])%numbers.Count;
  49.                     ShiftLeftBy(numbers, cmdArgs2);
  50.  
  51.                 }
  52.                 else if (command[0] == "Shift" &&command[1] == "right")
  53.                 {
  54.                     int cmdArgs2 = int.Parse(command[2])%numbers.Count;
  55.                     ShiftRightBy(numbers, cmdArgs2);
  56.                 }
  57.                
  58.                 command = Console.ReadLine().Split().ToArray();
  59.             }
  60.            
  61.             Console.WriteLine(string.Join(" ", numbers));
  62.  
  63.         }
  64.  
  65.  
  66.  
  67.  
  68.  
  69.          static void ShiftRightBy(List<int> numbers, int num)
  70.         {
  71.             for (int i = 0; i < num; i++)
  72.             {
  73.  
  74.                 numbers.Insert(0, numbers[numbers.Count - 1]);
  75.                 numbers.RemoveAt(numbers.Count - 1);
  76.  
  77.  
  78.             }
  79.         }
  80.  
  81.         static void ShiftLeftBy(List<int> numbers, int num)
  82.         {
  83.             for (int i = 0; i < num; i++)
  84.             {
  85.                 int temp = numbers[0];
  86.                 numbers.Add(temp);
  87.                 numbers.RemoveAt(0);
  88.             }
  89.         }
  90.  
  91.         static void Remove(List<int> numbers, int num)
  92.         {
  93.             if (num >= numbers.Count)
  94.             {
  95.                 Console.WriteLine("Invalid index");
  96.             }
  97.             else
  98.             {
  99.                 numbers.RemoveAt(num);
  100.             }
  101.         }
  102.  
  103.         static void Add(List<int> numbers, int num)
  104.         {
  105.             numbers.Add(num);
  106.         }
  107.        
  108.         static void Insert(List<int> numbers, int num1, int num2)
  109.         {
  110.             if (num2 >= numbers.Count)
  111.             {
  112.                 Console.WriteLine("Invalid index");
  113.             }
  114.             else
  115.             {
  116.                 numbers.Insert(num2, num1);
  117.             }
  118.         }
  119.  
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement