Advertisement
Guest User

7. List Manipulation Advanced

a guest
Feb 14th, 2021
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _6._List_Manipulation_Basics
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. List<int> num = Console.ReadLine()
  12. .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  13. .Select(int.Parse).ToList();
  14.  
  15. int counter = 0;
  16.  
  17. while (true)
  18. {
  19. string word = Console.ReadLine();
  20. if (word == "end")
  21. {
  22. break;
  23. }
  24.  
  25. string[] command = word.Split();
  26.  
  27. if (command[0] == "Add")
  28. {
  29. int toAdd = int.Parse(command[1]);
  30. num.Add(toAdd);
  31.  
  32. counter += 1;
  33. }
  34.  
  35. else if (command[0] == "Remove")
  36. {
  37. int toRemove = int.Parse(command[1]);
  38. num.Remove(toRemove);
  39. counter += 1;
  40.  
  41. }
  42.  
  43. else if (command[0] == "RemoveAt")
  44. {
  45. int removeAt = int.Parse(command[1]);
  46. num.RemoveAt(removeAt);
  47. counter += 1;
  48.  
  49. }
  50.  
  51. else if (command[0] == "Insert")
  52. {
  53. int insertNumber = int.Parse(command[1]);
  54. int indexLoction = int.Parse(command[2]);
  55. num.Insert(indexLoction, insertNumber);
  56. counter += 1;
  57.  
  58. }
  59.  
  60.  
  61.  
  62.  
  63.  
  64. if (command[0] == "Contains")
  65. {
  66. int contain = int.Parse(command[1]);
  67. bool isContain = false;
  68. foreach (var digit in num)
  69. {
  70.  
  71. if (digit == contain)
  72. {
  73. Console.WriteLine("Yes");
  74. isContain = true;
  75. break;
  76. }
  77. }
  78. if (isContain == false)
  79. {
  80. Console.WriteLine("No such number");
  81. }
  82. }
  83.  
  84. else if (command[0] == "PrintEven")
  85. {
  86. List<int> newNum = new List<int>();
  87. foreach (var digit in num)
  88. {
  89. if (digit % 2 == 0)
  90. {
  91. newNum.Add(digit);
  92. }
  93. }
  94. Console.WriteLine(string.Join(" ", newNum));
  95. }
  96.  
  97. else if (command[0] == "PrintOdd")
  98. {
  99. List<int> newNum = new List<int>();
  100. foreach (var digit in num)
  101. {
  102. if (digit % 2 != 0)
  103. {
  104. newNum.Add(digit);
  105. }
  106. }
  107. Console.WriteLine(string.Join(" ", newNum));
  108. }
  109.  
  110. else if (command[0] == "GetSum")
  111. {
  112. int sum = 0;
  113.  
  114. foreach (var digit in num)
  115. {
  116. sum += digit;
  117. }
  118. Console.WriteLine(sum);
  119. }
  120.  
  121. else if (command[0] == "Filter")
  122. {
  123. List<int> newNum = new List<int>();
  124.  
  125. if (command[1] == ">")
  126. {
  127. int index = int.Parse(command[2]);
  128. foreach (var digit in num)
  129. {
  130. if (digit > index)
  131. {
  132. newNum.Add(digit);
  133. }
  134. }
  135. }
  136.  
  137. else if (command[1] == ">=")
  138. {
  139. int index = int.Parse(command[2]);
  140. foreach (var digit in num)
  141. {
  142. if (digit >= index)
  143. {
  144. newNum.Add(digit);
  145. }
  146. }
  147. }
  148. else if (command[1] == "<")
  149. {
  150. int index = int.Parse(command[2]);
  151. foreach (var digit in num)
  152. {
  153. if (digit < index)
  154. {
  155. newNum.Add(digit);
  156. }
  157. }
  158. }
  159. Console.WriteLine(string.Join(" ", newNum));
  160.  
  161. }
  162.  
  163. }
  164. if (counter > 0)
  165. {
  166. Console.WriteLine(string.Join(" ", num));
  167.  
  168. }
  169. }
  170. }
  171. }
  172.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement