Advertisement
YavorJS

Array Manipulator - 66%

Oct 1st, 2016
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 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. nums = summed;
  57. return nums;
  58. }
  59.  
  60. private static void shift(List<int> nums, string[] commands)
  61. {
  62. int numberOfPositions = int.Parse(commands[1]);
  63. while (numberOfPositions > 0)
  64. {
  65. int first = nums[0];
  66. nums.RemoveAt(0);
  67. nums.Add(first);
  68. numberOfPositions--;
  69. }
  70. }
  71.  
  72. private static void remove(List<int> nums, string[] commands)
  73. {
  74. int index = int.Parse(commands[1]);
  75. nums.RemoveAt(index);
  76. }
  77.  
  78. private static void contains(List<int> nums, string[] commands)
  79. {
  80. int element = int.Parse(commands[1]);
  81. if (nums.Contains(element))
  82. {
  83. Console.WriteLine(nums.IndexOf(element));
  84. }
  85. else Console.WriteLine("-1");
  86. }
  87.  
  88. private static void addMany(List<int> nums, string[] commands)
  89. {
  90. int index = int.Parse(commands[1]);
  91. for (int i = commands.Length - 1; i >= 2; i--)
  92. {
  93. int element = int.Parse(commands[i]);
  94. nums.Insert(index, element);
  95. }
  96. }
  97.  
  98. private static void add(List<int> nums, string[] commands)
  99. {
  100. int index = int.Parse(commands[1]);
  101. int element = int.Parse(commands[2]);
  102. nums.Insert(index, element);
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement