Guest User

Lists - Exercises / 03. Array Manipulator

a guest
Jan 31st, 2017
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.40 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.             var command = Console.ReadLine().Split(' ').ToList();
  15.             while (command[0] !="print")
  16.             {
  17.                 switch (command[0])
  18.                 {
  19.                     case "add":
  20.                         {
  21.                             var index = int.Parse(command[1]);
  22.                             var element = int.Parse(command[2]);
  23.                             input.Insert(index, element);
  24.                             break;
  25.                         }
  26.                     case "remove":
  27.                         {
  28.                             var index = int.Parse(command[1]);
  29.                             input.RemoveAt(index);
  30.                             break;
  31.                         }
  32.                     case "contains":
  33.                         {
  34.                             var element = int.Parse(command[1]);
  35.                             int index = input.FindIndex(a => a == element);
  36.                             Console.WriteLine(index);
  37.                             break;
  38.                         }
  39.                     case "sumPairs":
  40.                         {
  41.                             for (int i = 0; i < input.Count; i++)
  42.                             {
  43.                                 input[i] += input[i + 1];
  44.                                 input.RemoveAt(i + 1);
  45.                             }
  46.                             break;
  47.                         }
  48.                     case "shift":
  49.                         {
  50.                             var positions = int.Parse(command[1]);
  51.                             RotateLeft(input, positions);
  52.                             break;
  53.                         }
  54.                     case "addMany":
  55.                         {
  56.                             var pos = int.Parse(command[1]);
  57.                             if (pos > input.Count)
  58.                             {
  59.                                 break;
  60.                             }
  61.                             for (int i = command.Count - 1; i >= 2; i--)
  62.                             {
  63.                                 input.Insert(pos, (int.Parse)(command[i]));
  64.                             }
  65.                             break;
  66.                         }
  67.                 }
  68.                 command = Console.ReadLine().Split(' ').ToList();
  69.             }
  70.  
  71.             Console.WriteLine($"[{string.Join(", ", input)}]");
  72.         }
  73.         public static void RotateLeftOnce(List<int> input)
  74.         {                                                            
  75.             var numToSwitch = input[0];                                      
  76.             for (var i = 0; i < input.Count; i += 1)                
  77.             {
  78.                 if (i + 1 < input.Count)
  79.                 {
  80.                     var switcher = input[i];
  81.                     input[i] = input[i + 1];
  82.                     input[i + 1] = switcher;
  83.                 }
  84.             }
  85.         }
  86.         public static void RotateLeft(List<int> input, int positions)
  87.         {
  88.             for (var i = 0; i < positions; i += 1)
  89.             {
  90.                 RotateLeftOnce(input);
  91.             }
  92.         }
  93.     }
  94. }
Add Comment
Please, Sign In to add comment