Advertisement
Guest User

Lists - Exercises / 03. Array Manipulator

a guest
Feb 1st, 2017
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Lists
  8. {
  9.     public class Lists
  10.     {
  11.         public static void Main()
  12.         {
  13.             var input = Console.ReadLine().Split(' ').Select(int.Parse).ToList();
  14.             while (true)
  15.             {
  16.                 var command = Console.ReadLine().Split(' ').ToArray();
  17.                 if (command[0] == "print")
  18.                 {
  19.                     break;
  20.                 }
  21.                 else if (command[0] == "add")
  22.                 {
  23.                     var index = int.Parse(command[1]);
  24.                     var element = int.Parse(command[2]);
  25.                     input.Insert(index, element);
  26.                 }
  27.                 else if (command[0] == "remove")
  28.                 {
  29.                     var index = int.Parse(command[1]);
  30.                     input.RemoveAt(index);
  31.                 }
  32.                 else if (command[0] == "contains")
  33.                 {
  34.                     var element = int.Parse(command[1]);
  35.                     int index = input.FindIndex(a => a == element);
  36.                     Console.WriteLine(index);
  37.                 }
  38.                 else if (command[0] == "sumPairs")
  39.                 {
  40.                     for (int i = 0; i < input.Count-1; i++)
  41.                     {
  42.                         input[i] += input[i + 1];
  43.                         input.RemoveAt(i + 1);
  44.                     }
  45.                 }
  46.                 else if (command[0] == "shift")
  47.                 {
  48.                     var positions = int.Parse(command[1]);
  49.                     RotateLeft(input, positions);
  50.                 }
  51.                 else if (command[0] == "addMany")
  52.                 {
  53.                     var position = int.Parse(command[1]);
  54.                     if (position > input.Count)
  55.                     {
  56.                         break;
  57.                     }
  58.                     for (int i = command.Length -1 ; i >= 2; i--)
  59.                     {
  60.                         input.Insert(position, int.Parse(command[i]));
  61.                     }
  62.                 }
  63.             }
  64.  
  65.             Console.WriteLine($"[{string.Join(", ", input)}]");
  66.         }
  67.         public static void RotateLeftOnce(List<int> input)
  68.         {
  69.             var numToSwitch = input[0];
  70.             for (var i = 0; i < input.Count; i += 1)
  71.             {
  72.                 if (i + 1 < input.Count)
  73.                 {
  74.                     var switcher = input[i];
  75.                     input[i] = input[i + 1];
  76.                     input[i + 1] = switcher;
  77.                 }
  78.             }
  79.         }
  80.         public static void RotateLeft(List<int> input, int positions)
  81.         {
  82.             for (var i = 0; i < positions; i += 1)
  83.             {
  84.                 RotateLeftOnce(input);
  85.             }
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement