Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- class Sample_Exam_II___June_2016
- {
- public static void Main()
- {
- List<List<int>> matrix = readMatrix();
- commandsProcessor(matrix);
- printMatrix(matrix);
- }
- private static void commandsProcessor(List<List<int>> matrix)
- {
- while (true)
- {
- List<string> commands = Console.ReadLine().Split().ToList();
- string command = commands[0];
- if (command == "end")
- {
- break;
- }
- else if (command == "swap")
- {
- swap(matrix, commands);
- }
- else if (command == "insert")
- {
- insert(matrix, commands);
- }
- else if (command == "remove")
- {
- remove(matrix, commands);
- }
- }
- }
- private static void remove(List<List<int>> matrix, List<string> commands)
- {
- string type = commands[1];
- string position = commands[2];
- int index = int.Parse(commands[3]);
- if (type == "even")
- {
- if (position == "row")
- {
- var temporary = matrix[index];
- temporary = temporary.Where(number => number % 2 != 0).ToList();
- matrix[index] = temporary;
- }
- else if (position == "col")
- {
- for (int row = 0; row < matrix.Count; row++)
- {
- if (index >= matrix[row].Count)
- {
- continue;
- }
- if (matrix[row][index] % 2 == 0)
- {
- matrix[row].RemoveAt(index);
- }
- }
- }
- }
- else if (type == "odd")
- {
- if (position == "row")
- {
- var temporary = matrix[index];
- temporary = temporary.Where(number => number % 2 == 0).ToList();
- matrix[index] = temporary;
- }
- else if (position == "col")
- {
- for (int row = 0; row < matrix.Count; row++)
- {
- if (index >= matrix[row].Count)
- {
- continue;
- }
- if (matrix[row][index] % 2 != 0)
- {
- matrix[row].RemoveAt(index);
- }
- }
- }
- }
- else if (type == "positive")
- {
- if (position == "row")
- {
- var temporary = matrix[index];
- temporary = temporary.Where(number => number % 2 < 0).ToList();
- matrix[index] = temporary;
- }
- else if (position == "col")
- {
- for (int row = 0; row < matrix.Count; row++)
- {
- if (index >= matrix[row].Count)
- {
- continue;
- }
- if (matrix[row][index] >= 0)
- {
- matrix[row].RemoveAt(index);
- }
- }
- }
- }
- else if (type == "negative")
- {
- if (position == "row")
- {
- var temporary = matrix[index];
- temporary = temporary.Where(number => number >= 0).ToList();
- matrix[index] = temporary;
- }
- else if (position == "col")
- {
- for (int row = 0; row < matrix.Count; row++)
- {
- if (index >= matrix[row].Count)
- {
- continue;
- }
- if (matrix[row][index] < 0)
- {
- matrix[row].RemoveAt(index);
- }
- }
- }
- }
- }
- private static void insert(List<List<int>> matrix, List<string> commands)
- {
- int row = int.Parse(commands[1]);
- int element = int.Parse(commands[2]);
- List<int> listInQuestion = matrix[row];
- listInQuestion.Insert(0, element);
- matrix[row] = listInQuestion;
- }
- private static void swap(List<List<int>> matrix, List<string> commands)
- {
- int first = int.Parse(commands[1]);
- int second = int.Parse(commands[2]);
- var firstRow = matrix[first];
- var secondRow = matrix[second];
- matrix[first] = secondRow;
- matrix[second] = firstRow;
- }
- private static void printMatrix(List<List<int>> matrix)
- {
- foreach (var row in matrix)
- {
- Console.WriteLine(string.Join(" ", row));
- }
- }
- private static List<List<int>> readMatrix()
- {
- int rows = int.Parse(Console.ReadLine());
- List<List<int>> matrix = new List<List<int>>();
- for (int row = 0; row < rows; row++)
- {
- matrix.Add(Console.ReadLine().Split().Select(int.Parse).ToList());
- }
- return matrix;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement