Teo_Yanchev

ListsExercise - 4.ListOperations - C# Fundamentals

Sep 19th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 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. List<string> command = Console.ReadLine()
  17. .Split()
  18. .ToList();
  19.  
  20. while (command[0] != "End")
  21. {
  22. string input = command[0];
  23.  
  24.  
  25. if (input == "Add")
  26. {
  27. int number = int.Parse(command[1]);
  28. numbers.Add(number);
  29. command = Console.ReadLine()
  30. .Split()
  31. .ToList();
  32. continue;
  33. }
  34.  
  35. else if (input == "Insert")
  36. {
  37. int number = int.Parse(command[1]);
  38. int index = int.Parse(command[2]);
  39.  
  40. if (index < 0 || (numbers.Count - 1) < index)
  41. {
  42. Console.WriteLine("Invalid index");
  43. }
  44.  
  45. else
  46. {
  47. numbers.Insert(index, number);
  48. }
  49.  
  50. command = Console.ReadLine()
  51. .Split()
  52. .ToList();
  53. continue;
  54. }
  55.  
  56. else if (input == "Remove")
  57. {
  58. // "NB" !! Here number represents the index !! "NB"
  59.  
  60. int number = int.Parse(command[1]);
  61.  
  62. if (number < 0 || (numbers.Count - 1) < number)
  63. {
  64. Console.WriteLine("Invalid index");
  65. }
  66.  
  67. else
  68. {
  69. numbers.RemoveAt(number);
  70. }
  71.  
  72. command = Console.ReadLine()
  73. .Split()
  74. .ToList();
  75. continue;
  76. }
  77.  
  78. else if (input == "Shift")
  79. {
  80. string inputTwo = command[1];
  81. int count = int.Parse(command[2]);
  82. if (inputTwo == "left")
  83. {
  84.  
  85. for (int i = 1; i <= count; i++)
  86. {
  87. int numSwitch = numbers[0];
  88. numbers.Insert(0, numbers[1]);
  89. numbers.RemoveAt(1);
  90. numbers.RemoveAt(1);
  91. numbers.Add(numSwitch);
  92. }
  93.  
  94. }
  95.  
  96. else
  97. {
  98. int numSwitch = 0;
  99. for (int i = 0; i < count; i++)
  100. {
  101. numSwitch = numbers[numbers.Count - 1];
  102. numbers.RemoveAt(numbers.Count - 1);
  103. numbers.Insert(0, numSwitch);
  104. }
  105. }
  106.  
  107. command = Console.ReadLine()
  108. .Split()
  109. .ToList();
  110. continue;
  111. }
  112.  
  113. }
  114. Console.WriteLine(string.Join(" ", numbers));
  115. }
  116. }
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment