Advertisement
NonaG

ArrayManipulator

Feb 2nd, 2017
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 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 ArrayManipulator
  8. {
  9. class ArrayManipulator
  10. {
  11. static void Main(string[] args)
  12. {
  13. var numbers = Console.ReadLine().Split().Select(int.Parse).ToList();
  14. string command = Console.ReadLine();
  15. while (command != "print")
  16. {
  17. List<string> commandList = command.Split().ToList();
  18. if (commandList[0] == "add")
  19. {
  20. int index = int.Parse(commandList[1]);
  21. int element = int.Parse(commandList[2]);
  22. numbers.Insert(index, element);
  23. }
  24. else if (commandList[0] == "addMany")
  25. {
  26. int index = int.Parse(commandList[1]);
  27. List<int> numsToAdd = new List<int>();
  28. for (int i = 2; i < commandList.Count; i++)
  29. {
  30. numsToAdd.Add(int.Parse(commandList[i]));
  31. }
  32. numbers.InsertRange(index, numsToAdd);
  33. }
  34. else if (commandList[0] == "contains")
  35. {
  36. int element = int.Parse(commandList[1]);
  37. if (numbers.Contains(element))
  38. {
  39. Console.WriteLine(numbers.IndexOf(element));
  40. }
  41. else
  42. {
  43. Console.WriteLine("-1");
  44. }
  45. }
  46. else if (commandList[0] == "remove")
  47. {
  48. int index = int.Parse(commandList[1]);
  49. numbers.RemoveAt(index);
  50. }
  51. else if (commandList[0] == "shift")
  52. {
  53. int positions = int.Parse(commandList[1]);
  54. for (int i = 0; i < positions; i++)
  55. {
  56. int temp = numbers[0];
  57. numbers.RemoveAt(0);
  58. numbers.Add(temp);
  59. }
  60. }
  61. else if (commandList[0] == "sumPairs")
  62. {
  63. numbers = SumPairs(numbers);
  64. }
  65. command = Console.ReadLine();
  66. }
  67. Console.WriteLine("[{0}]", string.Join(", ", numbers));
  68. }
  69.  
  70. static List<int> SumPairs(List<int> sums)
  71. {
  72. List<int> temp = new List<int>();
  73. for (int s = 0; s < 1; s++)
  74. {
  75. for (int i = 0; i < sums.Count - 1; i++)
  76. {
  77. temp.Add(sums[i] + sums[i + 1]);
  78. i = i + 1;
  79. }
  80. if (sums.Count % 2 != 0)
  81. {
  82. temp.Add(sums[sums.Count - 1]);
  83. }
  84. }
  85. return temp;
  86. }
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement