Advertisement
GabrielDas

Untitled

Feb 21st, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 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 P04ListOperations
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. List<int> numbers = Console.ReadLine().Split().Select(int.Parse).ToList();
  14.  
  15. while (true)
  16. {
  17. string command = Console.ReadLine();
  18. string[] commandLine = command.Split();
  19.  
  20. if (command == "End")
  21. {
  22. break;
  23. }
  24.  
  25. Commands(commandLine, numbers);
  26.  
  27. }
  28.  
  29.  
  30. Console.WriteLine(String.Join(" ", numbers));
  31.  
  32.  
  33. }
  34.  
  35. private static void Commands(string[] commandLine, List<int> numbers)
  36. {
  37.  
  38. if (commandLine[0]== ("Add"))
  39. {
  40. numbers.Add(int.Parse(commandLine[1]));
  41. }
  42. else if (commandLine[0]== ("Insert"))
  43. {
  44. int numberToInsert = int.Parse(commandLine[1]);
  45. int index = int.Parse(commandLine[2]);
  46.  
  47. if (index >= 0 && index < numbers.Count)
  48. {
  49.  
  50. numbers.Insert(index, numberToInsert);
  51. }
  52. else
  53. {
  54. Console.WriteLine("Invalid index");
  55. }
  56.  
  57. }
  58. else if (commandLine[0]==("Remove"))
  59. {
  60. int index = int.Parse(commandLine[1]);
  61.  
  62. if (index >= 0 && index < numbers.Count)
  63. {
  64. numbers.RemoveAt(index);
  65. }
  66. else
  67. {
  68. Console.WriteLine("Invalid index");
  69. }
  70. }
  71. else if (commandLine[0]==("Shift"))
  72. {
  73. string direction = commandLine[1];
  74. int count = int.Parse(commandLine[2]);
  75. Shift(numbers, direction, count);
  76. }
  77.  
  78. }
  79.  
  80. private static void Shift( List<int> numbers, string direction, int count)
  81. {
  82.  
  83. if (direction == "left")
  84. {
  85. for (int i = 0; i < count; i++)
  86. {
  87. int tempNumber = numbers[0];
  88. numbers.Add(tempNumber);
  89. numbers.RemoveAt(0);
  90.  
  91. }
  92. }
  93. else if (direction=="right")
  94. {
  95.  
  96. for (int i = 0; i < count; i++)
  97. {
  98. int tempNumber = numbers[numbers.Count - 1];
  99. numbers.Insert(0, tempNumber);
  100. numbers.RemoveAt(numbers.Count - 1);
  101.  
  102. }
  103. }
  104.  
  105. }
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement