Guest User

Array Manipulator

a guest
Oct 5th, 2016
89
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class ArrayManipulator
  6. {
  7. public static void Main()
  8. {
  9. List<int> numbers = Console.ReadLine().Split(' ').Select(int.Parse).ToList();
  10.  
  11. string[] input = Console.ReadLine().Split(' ');
  12. string command = input[0];
  13.  
  14. while (command != "print")
  15. {
  16. numbers = ExecutesCommand(numbers, command, input);
  17.  
  18. input = Console.ReadLine().Split(' ');
  19. command = input[0];
  20. }
  21.  
  22. Console.WriteLine("[" + string.Join(", ", numbers) + "]");
  23. }
  24.  
  25. static List<int> ExecutesCommand(List<int> numbers, string command, string[] input)
  26. {
  27.  
  28. switch (command)
  29. {
  30. case "add":
  31. int index = int.Parse(input[1]);
  32. int element = int.Parse(input[2]);
  33. numbers.Insert(index, element);
  34. break;
  35. case "addMany":
  36. index = int.Parse(input[1]);
  37. List<int> elements = new List<int>();
  38.  
  39. for (int i = 2; i < input.Length; i++)
  40. {
  41. elements.Add(int.Parse(input[i]));
  42. }
  43.  
  44. numbers.InsertRange(index, elements);
  45. break;
  46. case "contains":
  47. element = int.Parse(input[1]);
  48. index = numbers.IndexOf(element);
  49. Console.WriteLine(index);
  50. break;
  51. case "remove":
  52. index = int.Parse(input[1]);
  53. numbers.RemoveAt(index);
  54. break;
  55. case "shift":
  56. int positions = int.Parse(input[1]);
  57. List<int> demo = numbers.GetRange(positions, numbers.Count - positions);
  58. demo.AddRange(numbers.GetRange(0, positions));
  59. numbers = demo;
  60. break;
  61. case "sumPairs":
  62. for (int i = 0; i < numbers.Count - 1; i++)
  63. {
  64. numbers[i] += numbers[i + 1];
  65. numbers.Remove(numbers[i + 1]);
  66. }
  67. break;
  68. }
  69.  
  70. return numbers;
  71. }
  72. }
RAW Paste Data