pavlinpetkov88

Array Manipulator

Feb 6th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ArrayManipulator
  6. {
  7. class ArrayManipulator
  8. {
  9. static void Main(string[] args)
  10. {
  11. List<int> numbers = Console.ReadLine()
  12. .Split()
  13. .Select(int.Parse)
  14. .ToList();
  15.  
  16. bool canContinue = true;
  17.  
  18. while (canContinue)
  19. {
  20. List<string> commands = Console.ReadLine().Split().ToList();
  21.  
  22. switch (commands[0])
  23. {
  24. case "add":
  25. AddElements(numbers, commands);
  26. break;
  27. case "addMany":
  28. int indx = int.Parse(commands[1]);
  29. numbers.InsertRange(indx, commands.Skip(2).Select(int.Parse));
  30. break;
  31. case "contains":
  32. ContainsElement(numbers, commands);
  33. break;
  34. case "remove":
  35. numbers.RemoveAt(int.Parse(commands[1]));
  36. break;
  37. case "shift":
  38. ShiftElements(numbers, commands);
  39. break;
  40. case "sumPairs":
  41. SumPairs(numbers);
  42. break;
  43. case "print":
  44. Console.WriteLine("[{0}]",string.Join(", ", numbers));
  45. canContinue = false;
  46. break;
  47. }
  48. }
  49. }
  50.  
  51. private static void SumPairs(List<int> numbers)
  52. {
  53.  
  54. if (numbers.Count % 2 != 0)
  55. {
  56. numbers.Insert(numbers.Count - 1, 0);
  57. }
  58.  
  59. for (int i = 0; i < numbers.Count - 1; i++)
  60. {
  61. numbers[i] = numbers[i] + numbers[i + 1];
  62. numbers.RemoveAt(i + 1);
  63. }
  64.  
  65. }
  66.  
  67. private static void ShiftElements(List<int> numbers, List<string> commands)
  68. {
  69. int positions = int.Parse(commands[1]);
  70.  
  71. for (int i = 0; i < positions; i++)
  72. {
  73. int last = numbers[0];
  74.  
  75. numbers.RemoveAt(0);
  76. numbers.Add(last);
  77. }
  78. }
  79.  
  80.  
  81. private static void ContainsElement(List<int> numbers, List<string> commands)
  82. {
  83. int element = int.Parse(commands[1]);
  84.  
  85. if (numbers.Contains(element))
  86. {
  87. Console.WriteLine(numbers.IndexOf(element));
  88. }
  89. else
  90. {
  91. Console.WriteLine("-1");
  92. }
  93. }
  94.  
  95. private static void AddElements(List<int> numbers, List<string> commands)
  96. {
  97. int index = int.Parse(commands[1]);
  98. int element = int.Parse(commands[2]);
  99.  
  100. numbers.Insert(index, element);
  101. }
  102. }
  103. }
Add Comment
Please, Sign In to add comment