PetarNeshkov5360

List Operations

Apr 24th, 2021
1,195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace List_Operations_List_Exercise
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. List<int> Operations = Console.ReadLine().Split().Select(int.Parse).ToList();
  12. string input = string.Empty;
  13. while ((input = Console.ReadLine()) != "End")
  14. {
  15. string[] commands = input.Split();
  16. string action = commands[0];
  17. if (action == "Add")
  18. {
  19. int task = int.Parse(commands[1]);
  20. Operations.Add(task);
  21. }
  22. else if (action == "Insert")
  23. {
  24. int task = int.Parse(commands[1]);
  25. int index = int.Parse(commands[2]);
  26. if (index >= 0 && index < Operations.Count)
  27. {
  28. Operations.Insert(index, task);
  29. }
  30. else
  31. {
  32. Console.WriteLine("Invalid index");
  33. }
  34. }
  35. else if (action == "Shift")
  36. {
  37. string direction = commands[1];
  38. if (direction == "left")
  39. {
  40. int task = int.Parse(commands[2]);
  41. while (task != 0)
  42. {
  43. int currentnumber = Operations[0];
  44. Operations.RemoveAt(0);
  45. Operations.Add(currentnumber);
  46. task--;
  47. }
  48. }
  49. else if (direction == "right")
  50. {
  51. int task = int.Parse(commands[2]);
  52. while (task != 0)
  53. {
  54. int currentnumber = Operations[Operations.Count-1];
  55. Operations.RemoveAt(Operations.Count-1);
  56. Operations.Insert(0,currentnumber);
  57. task--;
  58. }
  59. }
  60. }
  61. else if (action == "Remove")
  62. {
  63. int task = int.Parse(commands[1]);
  64. if (task >= 0 && task < Operations.Count)
  65. {
  66. Operations.RemoveAt(task);
  67. }
  68. else
  69. {
  70. Console.WriteLine("Invalid index");
  71. }
  72. }
  73. }
  74. Console.WriteLine(String.Join(" ", Operations));
  75. }
  76. }
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment