Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.92 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _02._Excel_Functions
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int _rows = int.Parse(Console.ReadLine());
  11.  
  12.             var table = new string[_rows][];
  13.  
  14.             for (int row = 0; row < table.Length; row++)
  15.             {
  16.                 table[row] = Console.ReadLine().Split(", ", StringSplitOptions.RemoveEmptyEntries);
  17.             }
  18.  
  19.             var command = Console.ReadLine().Split(" ",StringSplitOptions.RemoveEmptyEntries);
  20.             var header = command[1];
  21.             var index = -1;
  22.  
  23.             for (int col = 0; col < table[0].Length; col++)
  24.             {
  25.                 if (table[0][col].Equals(header))
  26.                 {
  27.                     index = col;
  28.                     break;
  29.                 }
  30.             }
  31.  
  32.             var result = table;
  33.  
  34.             switch (command[0])
  35.             {
  36.                 case "hide":
  37.                     result = DeleteColumn(index, result);
  38.  
  39.                     foreach (var row in result)
  40.                     {
  41.                         Console.WriteLine(string.Join(" | ", row));
  42.                     }
  43.                     break;
  44.  
  45.                 case "sort":
  46.                     result = table.Skip(1).OrderBy(x => x[index]).ToArray();
  47.  
  48.                     Console.WriteLine(string.Join(" | ", table[0]));
  49.  
  50.                     foreach (var row in result)
  51.                     {
  52.                         Console.WriteLine(string.Join(" | ", row));
  53.                     }
  54.                     break;
  55.  
  56.                 case "filter":
  57.                     var value = command[2];
  58.                     result = table.Skip(1).Where(x => x.Contains(value)).ToArray();
  59.  
  60.                     Console.WriteLine(string.Join(" | ", table[0]));
  61.  
  62.                     for (int row = 1; row < table.Length; row++)
  63.                     {
  64.                         if (table[row][index] == value)
  65.                         {
  66.                             Console.WriteLine(string.Join(" | ", table[row]));
  67.                         }
  68.                     }
  69.                     break;
  70.             }
  71.         }
  72.  
  73.         public static string[][] DeleteColumn(int columnToDelete, string[][] originalArray)
  74.         {
  75.             var result = new string[originalArray.Length][];
  76.             for (int row = 0; row < originalArray.Length; row++)
  77.             {
  78.                 int skippedCol = 0;
  79.  
  80.                 result[row] = new string[originalArray[row].Length - 1];
  81.  
  82.                 for (int col = 0; col < originalArray[row].Length - 1; col++)
  83.                 {
  84.                     if (skippedCol == columnToDelete)
  85.                     {
  86.                         skippedCol++;
  87.                     }
  88.                     result[row][col] = originalArray[row][skippedCol];
  89.                     skippedCol++;
  90.                 }
  91.             }
  92.             return result;
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement