Advertisement
Guest User

14. Array Manipulator

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