Advertisement
miroslavbstoyanov

Untitled

Oct 14th, 2017
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 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. static void Main(string[] args)
  12. {
  13. int[] arrNumbers = Console.ReadLine()
  14. .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  15. .Select(int.Parse)
  16. .ToArray();
  17.  
  18. List<string> commands = new List<string>();
  19. string receivedCommands = "";
  20.  
  21. List<int> listOfNumbers = new List<int>(arrNumbers);
  22.  
  23. while(receivedCommands != "print")
  24. {
  25. commands = Console.ReadLine()
  26. .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  27. .ToList();
  28. receivedCommands = commands[0];
  29.  
  30. if (receivedCommands == "add")
  31. {
  32. int index = int.Parse(commands[1]);
  33. int element = int.Parse(commands[2]);
  34. listOfNumbers.Insert(index, element);
  35.  
  36. }
  37. else if (receivedCommands == "addMany")
  38. {
  39. int index = int.Parse(commands[1]);
  40.  
  41. for (int i = commands.Count - 1; i >= 2 ; i--)
  42. {
  43. int element = int.Parse(commands[i]);
  44. listOfNumbers.Insert(index, element);
  45. }
  46. }
  47. else if (receivedCommands == "contains")
  48. {
  49. int findNumber = int.Parse(commands[1]);
  50. if (listOfNumbers.Contains(findNumber))
  51. {
  52. for (int i = 0; i < listOfNumbers.Count; i++)
  53. {
  54. if (listOfNumbers[i] == findNumber)
  55. {
  56. Console.WriteLine(i);
  57. }
  58. }
  59. }
  60. else
  61. {
  62. Console.WriteLine(-1);
  63. }
  64. }
  65. else if (receivedCommands == "remove")
  66. {
  67. int removeNumber = int.Parse(commands[1]);
  68. listOfNumbers.RemoveAt(removeNumber);
  69. }
  70. else if (receivedCommands == "shift")
  71. {
  72. int shiftNumber = int.Parse(commands[1]);
  73. var result = listOfNumbers.GetRange(shiftNumber, listOfNumbers.Count - shiftNumber);
  74. result.AddRange(listOfNumbers.GetRange(0, shiftNumber));
  75. listOfNumbers = result;
  76. }
  77. else if (receivedCommands == "sumPairs")
  78. {
  79. int count = 0;
  80. List<int> listSum = new List<int>();
  81. for (int i = 0; i < listOfNumbers.Count; i+=2)
  82. {
  83. count++;
  84. if (count <= listOfNumbers.Count / 2)
  85. {
  86. listSum.Add(listOfNumbers[i] + listOfNumbers[i + 1]);
  87. }
  88. else
  89. {
  90. listSum.Add(listOfNumbers[i]);
  91. }
  92. }
  93. listOfNumbers = listSum;
  94. }
  95. else if (receivedCommands == "print")
  96. {
  97. Console.Write("[");
  98. Console.Write(string.Join(", ", listOfNumbers));
  99. Console.Write("]");
  100. Console.WriteLine();
  101.  
  102. }
  103. }
  104. }
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement