Advertisement
YavorJS

Matrix operator - 100%

Sep 18th, 2016
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 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.  
  8. class Sample_Exam_II___June_2016
  9. {
  10. public static void Main()
  11. {
  12. List<List<int>> matrix = readMatrix();
  13. commandsProcessor(matrix);
  14. printMatrix(matrix);
  15. }
  16.  
  17. private static void commandsProcessor(List<List<int>> matrix)
  18. {
  19. while (true)
  20. {
  21. List<string> commands = Console.ReadLine().Split().ToList();
  22. string command = commands[0];
  23. if (command == "end")
  24. {
  25. break;
  26. }
  27. else if (command == "swap")
  28. {
  29. swap(matrix, commands);
  30. }
  31. else if (command == "insert")
  32. {
  33. insert(matrix, commands);
  34. }
  35. else if (command == "remove")
  36. {
  37. remove(matrix, commands);
  38. }
  39. }
  40. }
  41.  
  42. private static void remove(List<List<int>> matrix, List<string> commands)
  43. {
  44. string type = commands[1];
  45. string position = commands[2];
  46. int index = int.Parse(commands[3]);
  47. if (type == "even")
  48. {
  49. if (position == "row")
  50. {
  51. var temporary = matrix[index];
  52. temporary = temporary.Where(number => number % 2 != 0).ToList();
  53. matrix[index] = temporary;
  54. }
  55. else if (position == "col")
  56. {
  57. for (int row = 0; row < matrix.Count; row++)
  58. {
  59. if (index >= matrix[row].Count)
  60. {
  61. continue;
  62. }
  63. if (matrix[row][index] % 2 == 0)
  64. {
  65. matrix[row].RemoveAt(index);
  66. }
  67. }
  68. }
  69. }
  70. else if (type == "odd")
  71. {
  72. if (position == "row")
  73. {
  74. var temporary = matrix[index];
  75. temporary = temporary.Where(number => number % 2 == 0).ToList();
  76. matrix[index] = temporary;
  77. }
  78. else if (position == "col")
  79. {
  80. for (int row = 0; row < matrix.Count; row++)
  81. {
  82. if (index >= matrix[row].Count)
  83. {
  84. continue;
  85. }
  86. if (matrix[row][index] % 2 != 0)
  87. {
  88. matrix[row].RemoveAt(index);
  89. }
  90. }
  91. }
  92. }
  93. else if (type == "positive")
  94. {
  95. if (position == "row")
  96. {
  97. var temporary = matrix[index];
  98. temporary = temporary.Where(number => number < 0).ToList();
  99. matrix[index] = temporary;
  100. }
  101. else if (position == "col")
  102. {
  103. for (int row = 0; row < matrix.Count; row++)
  104. {
  105. if (index >= matrix[row].Count)
  106. {
  107. continue;
  108. }
  109. if (matrix[row][index] >= 0)
  110. {
  111. matrix[row].RemoveAt(index);
  112. }
  113.  
  114. }
  115. }
  116. }
  117. else if (type == "negative")
  118. {
  119. if (position == "row")
  120. {
  121. var temporary = matrix[index];
  122. temporary = temporary.Where(number => number >= 0).ToList();
  123. matrix[index] = temporary;
  124. }
  125. else if (position == "col")
  126. {
  127. for (int row = 0; row < matrix.Count; row++)
  128. {
  129. if (index >= matrix[row].Count)
  130. {
  131. continue;
  132. }
  133. if (matrix[row][index] < 0)
  134. {
  135. matrix[row].RemoveAt(index);
  136. }
  137.  
  138. }
  139. }
  140. }
  141. }
  142.  
  143. private static void insert(List<List<int>> matrix, List<string> commands)
  144. {
  145. int row = int.Parse(commands[1]);
  146. int element = int.Parse(commands[2]);
  147. List<int> listInQuestion = matrix[row];
  148. listInQuestion.Insert(0, element);
  149. matrix[row] = listInQuestion;
  150. }
  151.  
  152. private static void swap(List<List<int>> matrix, List<string> commands)
  153. {
  154. int first = int.Parse(commands[1]);
  155. int second = int.Parse(commands[2]);
  156. var firstRow = matrix[first];
  157. var secondRow = matrix[second];
  158. matrix[first] = secondRow;
  159. matrix[second] = firstRow;
  160. }
  161.  
  162. private static void printMatrix(List<List<int>> matrix)
  163. {
  164. foreach (var row in matrix)
  165. {
  166. Console.WriteLine(string.Join(" ", row));
  167. }
  168. }
  169.  
  170. private static List<List<int>> readMatrix()
  171. {
  172. int rows = int.Parse(Console.ReadLine());
  173. List<List<int>> matrix = new List<List<int>>();
  174. for (int row = 0; row < rows; row++)
  175. {
  176. matrix.Add(Console.ReadLine().Split().Select(int.Parse).ToList());
  177. }
  178. return matrix;
  179. }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement