Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace StringMatrixRotation
- {
- using System;
- using System.Collections.Generic;
- using System.Text;
- public class Startup
- {
- public static void Main()
- {
- string rotateCommand = Console.ReadLine();
- int indexOfOpeningBracket = rotateCommand.IndexOf("(") + 1;
- int indexOfClosingBracket = rotateCommand.IndexOf(")");
- int degreesToRotateTo = int.Parse(rotateCommand.Substring(indexOfOpeningBracket,
- indexOfClosingBracket - indexOfOpeningBracket)) % 360;
- List<string> matrix = new List<string>();
- int maxLenghtStrFromMatrix = int.MinValue;
- string inputLine = Console.ReadLine();
- while (inputLine != "END")
- {
- matrix.Add(inputLine);
- if (inputLine.Length > maxLenghtStrFromMatrix)
- {
- maxLenghtStrFromMatrix = inputLine.Length;
- }
- inputLine = Console.ReadLine();
- }
- MakeAllTheStringsFromTheMatrixWithTheSameLenght(matrix, maxLenghtStrFromMatrix);
- int matrixRows = matrix.Count;
- int matrixCols = maxLenghtStrFromMatrix;
- int startRow = 0;
- Func<int, bool> endRowFunc = (index) => index < matrixRows;
- int startCol = 0;
- Func<int, bool> endColFunc = (index) => index < matrixCols;
- int rowIncrementor = 1;
- int colIncrementor = 1;
- Func<List<string>, int, int, char> takeElementFunc = (matr, row, col) => matr[row][col];
- if (degreesToRotateTo == 90)
- {
- startRow = 0;
- endRowFunc = (index) => index < matrixCols;
- startCol = matrixRows - 1;
- endColFunc = (index) => index >= 0;
- colIncrementor = -1;
- takeElementFunc = (matr, row, col) => matr[col][row];
- }
- else if (degreesToRotateTo == 180)
- {
- startRow = matrixRows - 1;
- endRowFunc = (index) => index >= 0;
- startCol = matrixCols - 1;
- endColFunc = (index) => index >= 0;
- rowIncrementor = -1;
- colIncrementor = -1;
- }
- else if (degreesToRotateTo == 270)
- {
- startRow = matrixCols - 1;
- endRowFunc = (index) => index >= 0;
- endColFunc = (index) => index < matrixRows;
- rowIncrementor = -1;
- takeElementFunc = (matr, row, col) => matr[col][row];
- }
- StringBuilder result = new StringBuilder();
- for (int currRow = startRow; endRowFunc(currRow); currRow += rowIncrementor)
- {
- for (int currCol = startCol; endColFunc(currCol); currCol += colIncrementor)
- {
- result.Append(takeElementFunc(matrix, currRow, currCol));
- }
- result.AppendLine();
- }
- Console.Write(result);
- }
- private static void MakeAllTheStringsFromTheMatrixWithTheSameLenght(List<string> matrix, int maxLenghtStrFromMatrix)
- {
- for (int currRow = 0; currRow < matrix.Count; currRow++)
- {
- matrix[currRow] = matrix[currRow].PadRight(maxLenghtStrFromMatrix);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement