Advertisement
simonradev

StringMatrixRotation

May 5th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.52 KB | None | 0 0
  1. namespace StringMatrixRotation
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Text;
  6.  
  7.     public class Startup
  8.     {
  9.         public static void Main()
  10.         {
  11.             string rotateCommand = Console.ReadLine();
  12.  
  13.             int indexOfOpeningBracket = rotateCommand.IndexOf("(") + 1;
  14.             int indexOfClosingBracket = rotateCommand.IndexOf(")");
  15.             int degreesToRotateTo = int.Parse(rotateCommand.Substring(indexOfOpeningBracket,
  16.                                                                       indexOfClosingBracket - indexOfOpeningBracket)) % 360;
  17.  
  18.             List<string> matrix = new List<string>();
  19.             int maxLenghtStrFromMatrix = int.MinValue;
  20.  
  21.             string inputLine = Console.ReadLine();
  22.             while (inputLine != "END")
  23.             {
  24.                 matrix.Add(inputLine);
  25.  
  26.                 if (inputLine.Length > maxLenghtStrFromMatrix)
  27.                 {
  28.                     maxLenghtStrFromMatrix = inputLine.Length;
  29.                 }
  30.  
  31.                 inputLine = Console.ReadLine();
  32.             }
  33.  
  34.             MakeAllTheStringsFromTheMatrixWithTheSameLenght(matrix, maxLenghtStrFromMatrix);
  35.  
  36.             int matrixRows = matrix.Count;
  37.             int matrixCols = maxLenghtStrFromMatrix;
  38.  
  39.             int startRow = 0;
  40.             Func<int, bool> endRowFunc = (index) => index < matrixRows;
  41.  
  42.             int startCol = 0;
  43.             Func<int, bool> endColFunc = (index) => index < matrixCols;
  44.  
  45.             int rowIncrementor = 1;
  46.             int colIncrementor = 1;
  47.  
  48.             Func<List<string>, int, int, char> takeElementFunc = (matr, row, col) => matr[row][col];
  49.  
  50.             if (degreesToRotateTo == 90)
  51.             {
  52.                 startRow = 0;
  53.                 endRowFunc = (index) => index < matrixCols;
  54.  
  55.                 startCol = matrixRows - 1;
  56.                 endColFunc = (index) => index >= 0;
  57.  
  58.                 colIncrementor = -1;
  59.  
  60.                 takeElementFunc = (matr, row, col) => matr[col][row];
  61.             }
  62.             else if (degreesToRotateTo == 180)
  63.             {
  64.                 startRow = matrixRows - 1;
  65.                 endRowFunc = (index) => index >= 0;
  66.  
  67.                 startCol = matrixCols - 1;
  68.                 endColFunc = (index) => index >= 0;
  69.  
  70.                 rowIncrementor = -1;
  71.                 colIncrementor = -1;
  72.             }
  73.             else if (degreesToRotateTo == 270)
  74.             {
  75.                 startRow = matrixCols - 1;
  76.                 endRowFunc = (index) => index >= 0;
  77.  
  78.                 endColFunc = (index) => index < matrixRows;
  79.  
  80.                 rowIncrementor = -1;
  81.  
  82.                 takeElementFunc = (matr, row, col) => matr[col][row];
  83.             }
  84.  
  85.             StringBuilder result = new StringBuilder();
  86.             for (int currRow = startRow; endRowFunc(currRow); currRow += rowIncrementor)
  87.             {
  88.                 for (int currCol = startCol; endColFunc(currCol); currCol += colIncrementor)
  89.                 {
  90.                     result.Append(takeElementFunc(matrix, currRow, currCol));
  91.                 }
  92.  
  93.                 result.AppendLine();
  94.             }
  95.  
  96.             Console.Write(result);
  97.         }
  98.  
  99.         private static void MakeAllTheStringsFromTheMatrixWithTheSameLenght(List<string> matrix, int maxLenghtStrFromMatrix)
  100.         {
  101.             for (int currRow = 0; currRow < matrix.Count; currRow++)
  102.             {
  103.                 matrix[currRow] = matrix[currRow].PadRight(maxLenghtStrFromMatrix);
  104.             }
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement