Advertisement
zarkoy

Untitled

Oct 25th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _04_ListOperations
  6. {
  7. class Program
  8. {
  9. public static void Main()
  10. {
  11. List<int> numbers = Console.ReadLine()
  12. .Split()
  13. .Select(int.Parse)
  14. .ToList();
  15.  
  16. while (true)
  17. {
  18. List<string> command = Console.ReadLine()
  19. .ToLower()
  20. .Split()
  21. .ToList();
  22.  
  23. string operation = command[0];
  24.  
  25. if (operation == "end")
  26. {
  27. break;
  28. }
  29. else if (operation == "add")
  30. {
  31. int currentNumber = int.Parse(command[1]);
  32. numbers.Add(currentNumber);
  33. }
  34. else if (operation == "insert")
  35. {
  36.  
  37. int position = int.Parse(command[2]);
  38. if (position > numbers.Count - 1)
  39. {
  40. Console.WriteLine("Invalid index");
  41. }
  42. else
  43. {
  44. int numbToInsert = int.Parse(command[1]);
  45. numbers.Insert(position, numbToInsert);
  46. }
  47.  
  48. }
  49. else if (operation == "remove")
  50. {
  51. int indexToRemove = int.Parse(command[1]);
  52. if (indexToRemove > numbers.Count - 1)
  53. {
  54. Console.WriteLine("Invalid index");
  55. }
  56. else
  57. {
  58. numbers.RemoveAt(indexToRemove);
  59. }
  60.  
  61. }
  62. else if (operation == "shift")
  63. {
  64. if (command[1] == "right")
  65. {
  66. int count = int.Parse(command[2]);
  67. int last = numbers.Count - 1;
  68.  
  69. for (int i = 0; i < count; i++)
  70. {
  71. int moveToFront = numbers[last];
  72. numbers.RemoveAt(last);
  73. numbers.Insert(0, moveToFront);
  74. }
  75. }
  76. else if (command[1] == "left")
  77. {
  78. int count = int.Parse(command[2]);
  79. int last = numbers.Count - 1;
  80.  
  81. for (int i = 0; i < count; i++)
  82. {
  83. int moveToEnd = numbers[0];
  84. numbers.RemoveAt(0);
  85. numbers.Insert(last, moveToEnd);
  86. }
  87. }
  88. }
  89. }
  90. Console.WriteLine(string.Join(" ", numbers));
  91. }
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement