Advertisement
MZlatev

Untitled

Oct 25th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _4._ListOperations
  6. {
  7. class Program
  8. {
  9. static void Main()
  10. {
  11. List<int> numbers = Console.ReadLine()
  12. .Split(" ")
  13. .Select(int.Parse)
  14. .ToList();
  15.  
  16. string input;
  17.  
  18. while ((input = Console.ReadLine()) != "End")
  19. {
  20. string[] splitedInput = input.Split(" ");
  21.  
  22. string command = splitedInput[0];
  23.  
  24. switch (command)
  25. {
  26. case "Add":
  27. int number = int.Parse(splitedInput[1]);
  28. numbers.Add(number);
  29. break;
  30.  
  31. case "Insert":
  32. number = int.Parse(splitedInput[1]);
  33. int index = int.Parse(splitedInput[2]);
  34.  
  35. bool isIndexValid = IsIndexValid(index, 0, numbers.Count);
  36.  
  37. if (isIndexValid)
  38. {
  39. numbers.Insert(index, number);
  40. }
  41.  
  42. break;
  43.  
  44. case "Remove":
  45. index = int.Parse(splitedInput[1]);
  46.  
  47. isIndexValid = IsIndexValid(index, 0, numbers.Count - 1);
  48.  
  49. if (isIndexValid)
  50. {
  51. numbers.RemoveAt(index);
  52. }
  53.  
  54. break;
  55.  
  56. case "Shift":
  57.  
  58. string direction = splitedInput[1];
  59. int count = int.Parse(splitedInput[2]);
  60.  
  61. if (direction == "left")
  62. {
  63.  
  64. for (int i = 0; i < count; i++)
  65. {
  66. numbers.Add(numbers[0]);
  67. numbers.RemoveAt(0);
  68. }
  69.  
  70. //for (int i = 0; i < count; i++)
  71. //{
  72. // int temp = numbers[0];
  73.  
  74. // for (int j = 0; j < numbers.Count - 1; j++)
  75. // {
  76. // numbers[j] = numbers[j + 1];
  77. // }
  78.  
  79. // numbers[numbers.Count - 1] = temp;
  80. //}
  81. }
  82.  
  83. else if (direction == "right")
  84. {
  85. for (int i = 0; i < count; i++)
  86. {
  87. numbers.Insert(0, numbers[numbers.Count - 1]);
  88. numbers.RemoveAt(numbers.Count - 1);
  89. }
  90.  
  91. //for (int i = 0; i < count; i++)
  92. //{
  93. // int temp = numbers[numbers.Count - 1];
  94.  
  95. // for (int j = numbers.Count - 2; j >= 0; j--)
  96. // {
  97. // numbers[j + 1] = numbers[j];
  98. // }
  99.  
  100. // numbers[0] = temp;
  101. //}
  102.  
  103. }
  104.  
  105.  
  106. break;
  107.  
  108. default:
  109. break;
  110. }
  111. }
  112.  
  113. Console.WriteLine(string.Join(" ", numbers));
  114. }
  115.  
  116. static bool IsIndexValid(int index, int minRange, int maxRange)
  117. {
  118. if (index < minRange || index > maxRange)
  119. {
  120. Console.WriteLine("Invalid index");
  121. return false;
  122. }
  123.  
  124. return true;
  125. }
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement