Guest User

Untitled

a guest
Aug 10th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.64 KB | None | 0 0
  1. namespace _02.MatrixOperator
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6.  
  7.     public class Program
  8.     {
  9.        public static void Main()
  10.         {
  11.             int rows = int.Parse(Console.ReadLine());
  12.  
  13.             List<List<int>> matrix= new List<List<int>>();
  14.  
  15.             for (int i = 0; i < rows; i++)
  16.             {
  17.                 matrix.Add(Console.ReadLine().Split().Select(int.Parse).ToList());
  18.  
  19.             }
  20.             Commands(matrix);
  21.         }
  22.  
  23.         private static void Commands(List<List<int>> matrix)
  24.         {
  25.             string command = Console.ReadLine();
  26.             while (command!="end")
  27.             {
  28.                 string[] tokens = command.Split();
  29.                 string commandName = tokens[0];
  30.                 string firstParameter = tokens[1];
  31.                 string secondParameter = tokens[2];
  32.  
  33.                 switch (commandName)
  34.                 {
  35.                     case "swap":
  36.                         Swap(matrix, firstParameter, secondParameter);
  37.                         break;
  38.                     case "insert":
  39.                         Insert(matrix, firstParameter, secondParameter);
  40.                         break;
  41.                     case "remove":
  42.                         Remove(matrix, firstParameter, secondParameter, int.Parse(tokens[3]));
  43.                         break;
  44.                 }
  45.                 command = Console.ReadLine();
  46.             }
  47.             PrintMatrix(matrix);
  48.  
  49.         }
  50.  
  51.         private static void PrintMatrix(List<List<int>> matrix)
  52.         {
  53.             foreach (var row in matrix)
  54.             {
  55.                 Console.WriteLine(string.Join(" ", row));
  56.             }
  57.         }
  58.  
  59.  
  60.         private static void Remove(List<List<int>> matrix, string type, string position, int index)
  61.         {
  62.  
  63.             if (position=="row")
  64.             {
  65.                 switch (type)
  66.                 {
  67.                     case "positive":
  68.                         matrix[index] = matrix[index].Where(n => n < 0).ToList();
  69.                         break;
  70.                     case "negative":
  71.                         matrix[index] = matrix[index].Where(n => n >= 0).ToList();
  72.                         break;
  73.                     case "odd":
  74.                         matrix[index] = matrix[index].Where(n => n % 2 == 0).ToList();
  75.                         break;
  76.                     case "even":
  77.                         matrix[index] = matrix[index].Where(n => n % 2 != 0).ToList();
  78.                         break;
  79.                 }
  80.             }
  81.             else
  82.             {
  83.                 switch (type)
  84.                 {
  85.                     case "positive":
  86.                         for (int currentRow = 0; currentRow < matrix.Count; currentRow++)
  87.                         {
  88.                             if (index >= matrix[currentRow].Count)
  89.                             {
  90.                                 continue;
  91.                             }
  92.                             if (matrix[currentRow][index]>=0)
  93.                             {
  94.                                 matrix[currentRow].RemoveAt(index);
  95.                             }
  96.  
  97.                         }
  98.                         break;
  99.                     case "negative":
  100.                         for (int currentRow = 0; currentRow < matrix.Count; currentRow++)
  101.                         {
  102.                             if (index >= matrix[currentRow].Count)
  103.                             {
  104.                                 continue;
  105.                             }
  106.                             if (matrix[currentRow][index] < 0)
  107.                             {
  108.                                 matrix[currentRow].RemoveAt(index);
  109.                             }
  110.  
  111.                         }
  112.                         break;
  113.                     case "odd":
  114.                         for (int currentRow = 0; currentRow < matrix.Count; currentRow++)
  115.                         {
  116.                             if (index >= matrix[currentRow].Count)
  117.                             {
  118.                                 continue;
  119.                             }
  120.                             if (matrix[currentRow][index] %2 != 0)
  121.                             {
  122.                                 matrix[currentRow].RemoveAt(index);
  123.                             }
  124.  
  125.                         }
  126.                         break;
  127.                     case "even":
  128.                         for (int currentRow = 0; currentRow < matrix.Count; currentRow++)
  129.                         {
  130.                             if (index >= matrix[currentRow].Count)
  131.                             {
  132.                                 continue;
  133.                             }
  134.                             if (matrix[currentRow][index] % 2 == 0)
  135.                             {
  136.                                 matrix[currentRow].RemoveAt(index);
  137.                             }
  138.  
  139.                         }
  140.                         break;
  141.                 }
  142.                
  143.             }
  144.         }
  145.  
  146.         private static void Insert(List<List<int>> matrix, string firstParameter, string secondParameter)
  147.         {
  148.             int rowIndex = int.Parse(firstParameter);
  149.             int number = int.Parse(secondParameter);
  150.             matrix[rowIndex].Insert(0, number);
  151.         }
  152.  
  153.         private static void Swap(List<List<int>> matrix, string firstParameter, string secondParameter)
  154.         {
  155.             int firstRow = int.Parse(firstParameter);
  156.             int secondRow = int.Parse(secondParameter);
  157.  
  158.             List<int> temporary = matrix[firstRow];
  159.             matrix[firstRow] = matrix[secondRow];
  160.             matrix[secondRow] = temporary;
  161.         }
  162.     }
  163. }
Add Comment
Please, Sign In to add comment