diduan

Array_Manipulator_SoftUni_Excs

Mar 24th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 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 _05_Array_Manipulator
  8. {
  9. class Program
  10. {
  11.  
  12. static void Main(string[] args)
  13. {
  14.  
  15. List<int> nums = Console.ReadLine()
  16. .Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
  17. .Select(int.Parse)
  18. .ToList();
  19.  
  20. string[] com = Console.ReadLine()
  21. .ToLower()
  22. .Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
  23. .ToArray(); ;
  24.  
  25.  
  26. while (com[0]!="print")
  27. {
  28. switch (com[0])
  29. {
  30. case "add":
  31. int index1 = int.Parse(com[1]);
  32. int element1 = int.Parse(com[2]);
  33. nums.Insert(index1,element1);
  34. break;
  35.  
  36. case "addmany":
  37. int index2 = int.Parse(com[1]);
  38. int[] elements2 = new int[com.Length - 2];
  39. for (int i = 2; i < com.Length; i++)
  40. {
  41. elements2[i - 2] = int.Parse(com[i]);
  42. }
  43. nums.InsertRange(index2, elements2);
  44. break;
  45.  
  46. case "contains":
  47.  
  48. int element3 = int.Parse(com[1]);
  49. bool contains = nums.Contains(element3);
  50. if (contains)
  51. {
  52. Console.WriteLine(nums.FindIndex(x => x == element3));
  53. }
  54. else
  55. {
  56. Console.WriteLine("-1");
  57. }
  58.  
  59. break;
  60.  
  61. case "remove":
  62. int index4 = int.Parse(com[1]);
  63. if (index4>nums.Count-1 || index4<0)
  64. {
  65. Console.WriteLine("[" + string.Join(", ", nums) + "]");
  66. return;
  67. }
  68. nums.RemoveAt(index4);
  69. break;
  70.  
  71. case "shift":
  72. int position = int.Parse(com[1]);
  73. if (position > nums.Count)
  74. {
  75. position = position % nums.Count;
  76. }
  77. int left = position;
  78. int right = nums.Count - left;
  79. List<int> listLeft = nums.Take(left).ToList();
  80. List<int> listRight = nums;
  81. listRight.RemoveRange(0, left);
  82. listRight.AddRange(listLeft);
  83. nums = listRight;
  84. break;
  85.  
  86. case "sumpairs":
  87.  
  88. List<int> even = nums.Where((x, i) => i % 2 == 0).ToList();
  89. List<int> odd = nums.Where((x, i) => i % 2 != 0).ToList();
  90.  
  91. if (even.Count > odd.Count)
  92. {
  93. odd.Add(0);
  94. }
  95. else
  96. {
  97. even.Add(0);
  98. }
  99. nums.Clear();
  100. nums = even.Zip(odd, (x, y) => x + y).ToList();
  101. break;
  102.  
  103. //default:
  104. // Console.WriteLine("Incorrect input");
  105. // return;
  106.  
  107. }
  108. string[] comW = Console.ReadLine()
  109. .ToLower()
  110. .Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
  111. .ToArray(); ;
  112. com = comW;
  113. }
  114. nums.TrimExcess();
  115. Console.WriteLine("["+string.Join(", ",nums)+"]");
  116.  
  117.  
  118. }
  119. }
  120. }
Add Comment
Please, Sign In to add comment