Advertisement
YavorJS

Array Manipulator

Oct 2nd, 2016
872
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 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.  
  8. class Array_Manipulator
  9. {
  10. static void Main()
  11. {
  12. List<int> nums = Console.ReadLine().Split().Select(int.Parse).ToList();
  13.  
  14. while (true)
  15. {
  16. string[] commands = Console.ReadLine().Split().ToArray();
  17. string command = commands[0];
  18. if (command == "print") break;
  19. if (command == "add")
  20. {
  21. add(nums, commands);
  22. }
  23. else if (command == "addMany")
  24. {
  25. addMany(nums, commands);
  26. }
  27. else if (command == "contains")
  28. {
  29. contains(nums, commands);
  30. }
  31. else if (command == "remove")
  32. {
  33. remove(nums, commands);
  34. }
  35. else if (command == "shift")
  36. {
  37. shift(nums, commands);
  38. }
  39. else if (command == "sumPairs")
  40. {
  41. nums = sumPairs(nums);
  42. }
  43. }
  44. Console.WriteLine("[" + string.Join(", ", nums) + "]");
  45. }
  46.  
  47. private static List<int> sumPairs(List<int> nums)
  48. {
  49. List<int> summed = new List<int>();
  50. while (nums.Count >= 2)
  51. {
  52. summed.Add(nums[0] + nums[1]);
  53. nums.RemoveAt(0);
  54. nums.RemoveAt(0);
  55. }
  56. if (nums.Count==1) summed.Add(nums[0]);
  57.  
  58. nums = summed;
  59. return nums;
  60. }
  61.  
  62. private static void shift(List<int> nums, string[] commands)
  63. {
  64. int numberOfPositions = int.Parse(commands[1]);
  65. while (numberOfPositions > 0)
  66. {
  67. int first = nums[0];
  68. nums.RemoveAt(0);
  69. nums.Add(first);
  70. numberOfPositions--;
  71. }
  72. }
  73.  
  74. private static void remove(List<int> nums, string[] commands)
  75. {
  76. int index = int.Parse(commands[1]);
  77. nums.RemoveAt(index);
  78. }
  79.  
  80. private static void contains(List<int> nums, string[] commands)
  81. {
  82. int element = int.Parse(commands[1]);
  83. if (nums.Contains(element))
  84. {
  85. Console.WriteLine(nums.IndexOf(element));
  86. }
  87. else Console.WriteLine("-1");
  88. }
  89.  
  90. private static void addMany(List<int> nums, string[] commands)
  91. {
  92. int index = int.Parse(commands[1]);
  93. for (int i = commands.Length - 1; i >= 2; i--)
  94. {
  95. int element = int.Parse(commands[i]);
  96. nums.Insert(index, element);
  97. }
  98. }
  99.  
  100. private static void add(List<int> nums, string[] commands)
  101. {
  102. int index = int.Parse(commands[1]);
  103. int element = int.Parse(commands[2]);
  104. nums.Insert(index, element);
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement