Advertisement
Guest User

Untitled

a guest
Jun 7th, 2016
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class ArrayManipulator
  6. {
  7. static void Main(string[] args)
  8. {
  9. List<int> numbers= Console.ReadLine().Split(' ').Select(int.Parse).ToList();
  10.  
  11. string command = "";
  12.  
  13. while (command != "print")
  14. {
  15. string[] input = Console.ReadLine().Split(' ').ToArray();
  16.  
  17. command = input[0];
  18. int index = 0;
  19. int element = 0;
  20.  
  21. switch (command)
  22. {
  23. case "add":
  24. index = Convert.ToInt32(input[1]);
  25. element = Convert.ToInt32(input[2]);
  26. Add(numbers, index, element);
  27. break;
  28. case "addMany":
  29. index = Convert.ToInt32(input[1]);
  30. int[] elements = new int[input.Length-2];
  31. for (int i = 2; i < input.Length; i++)
  32. {
  33. elements[i-2]= Convert.ToInt32(input[i]);
  34. }
  35. AddManyElements(numbers, index, elements);
  36. break;
  37. case "contains":
  38. element = Convert.ToInt32(input[1]);
  39. Console.WriteLine(Contains(numbers, element));
  40. break;
  41. case "remove":
  42. index = Convert.ToInt32(input[1]);
  43. RemovesElement(numbers, index);
  44. break;
  45. case "shift":
  46. int positions = Convert.ToInt32(input[1]);
  47. numbers = ShiftElementsWithNPositions(numbers, positions);
  48. break;
  49. case "sumPairs":
  50. SumPairs(numbers);
  51. break;
  52. case "print":
  53. PrintArray(numbers);
  54. break;
  55. default:
  56. break;
  57. }
  58.  
  59. }
  60.  
  61. }
  62.  
  63. static List<int> SumPairs(List<int> numbers)
  64. {
  65. if(numbers.Count % 2 == 0)
  66. {
  67. for (int i = 0; i < numbers.Count-1; i++)
  68. {
  69. int sum = numbers[i] + numbers[i + 1];
  70. numbers[i] = sum;
  71. numbers.RemoveAt(i + 1);
  72. }
  73. }
  74. else
  75. {
  76. for (int i = 0; i < numbers.Count-2; i++)
  77. {
  78. int sum = numbers[i] + numbers[i + 1];
  79. numbers[i] = sum;
  80. numbers.RemoveAt(i + 1);
  81. }
  82. }
  83. return numbers;
  84. }
  85.  
  86. static List<int> ShiftElementsWithNPositions(List<int> nums, int positions)
  87. {
  88. int[] numsArr = new int[nums.Count];
  89. for (int currentPosition = 0; currentPosition < positions; currentPosition++)
  90. {
  91. int firstElement = nums[0];
  92. for (int i = 0; i < nums.Count-1; i++)
  93. {
  94. numsArr[i] = nums[i+1];
  95. }
  96. numsArr[numsArr.Length-1]=firstElement;
  97. nums = new List<int>(numsArr);
  98. }
  99. return nums;
  100. }
  101.  
  102. static List<int> RemovesElement(List<int> numbers, int index)
  103. {
  104. numbers.RemoveAt(index);
  105. return numbers;
  106. }
  107.  
  108. static int Contains(List<int> numbers, int element)
  109. {
  110. int result = -1;
  111. if (numbers.Contains(element))
  112. result = numbers.IndexOf(element);
  113. return result;
  114. }
  115.  
  116. static List<int> AddManyElements(List<int> numbers, int index, int[] elements)
  117. {
  118. for (int i = elements.Length-1; i >= 0; i--)
  119. {
  120. numbers.Insert(index, elements[i]);
  121. }
  122. return numbers;
  123. }
  124.  
  125. static List<int> Add(List<int> numbers, int index, int element)
  126. {
  127. numbers.Insert(index, element);
  128. return numbers;
  129. }
  130.  
  131. static void PrintArray(List<int> numbers)
  132. {
  133. Console.WriteLine("[" + string.Join(", ",numbers) + "]");
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement