Guest User

Untitled

a guest
Jul 8th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace _05.Array_Manipulator
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. List<int> inputList = Console.ReadLine()
  12. .Split(' ').Select(int.Parse).ToList();
  13. string command;
  14.  
  15.  
  16.  
  17. while ((command = Console.ReadLine()) != "print")
  18. {
  19. string[] commands = command.Split();
  20.  
  21. if (commands[0] == "add")
  22. {
  23. int index = int.Parse(commands[1]);
  24. int elementValue = int.Parse(commands[2]);
  25. inputList.Insert(index, elementValue);
  26. }
  27. else if (commands[0] == "addMany")
  28. {
  29. int index = int.Parse(commands[1]);
  30. List<int> elementsToAdd = commands
  31. .Skip(2).Select(int.Parse).ToList();
  32. inputList.InsertRange(index, elementsToAdd);
  33. }
  34. else if (commands[0] == "contains")
  35. {
  36. int element = int.Parse(commands[1]); //added
  37. if (inputList.Contains(element)) //changed
  38. {
  39. Console.WriteLine(inputList.IndexOf(element)); //changed
  40. }
  41. else
  42. {
  43. Console.WriteLine(-1);
  44. }
  45. }
  46. else if (commands[0] == "remove")
  47. {
  48. inputList.RemoveAt(int.Parse(commands[1]));
  49. }
  50. else if (commands[0] == "shift")
  51. {
  52. int index = int.Parse(commands[1]);
  53. //for (int j = 0; j < inputList.Count; j++)
  54. //{
  55. List<int> firstPartOfList = new List<int> { };
  56. for (int i = 0; i < index; i++)
  57. {
  58. firstPartOfList.Add(inputList[i]);
  59. }
  60. int j = 0;
  61. for (int i = index; i < inputList.Count; i++)
  62. {
  63. inputList[j] = inputList[i];
  64. j++;
  65. }
  66. for (int i = 0; i < index; i++)
  67. {
  68. inputList[j] = firstPartOfList[i];
  69. j++;
  70. }
  71. //}
  72. }
  73. else if (commands[0] == "sumPairs")
  74. {
  75. if (inputList.Count % 2 == 1) inputList.Add(0); //added
  76. int iterations = inputList.Count / 2; //added
  77. for (int i = 0; i < iterations; i++) //changed
  78. {
  79. inputList[i] = inputList[i] + inputList[i + 1];
  80. inputList.RemoveAt(i + 1);
  81. }
  82. }
  83. }
  84. Console.Write("[");
  85. for (int i = 0; i < inputList.Count; i++)
  86. {
  87. if (i == inputList.Count - 1)
  88. Console.Write(inputList[i]);
  89. else
  90. {
  91. Console.Write(inputList[i] + ", ");
  92. }
  93. }
  94. Console.WriteLine("]");
  95. }
  96. }
  97. }
Add Comment
Please, Sign In to add comment