Advertisement
dimipan80

String Matrix Rotation

May 11th, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.80 KB | None | 0 0
  1. /* You are given a sequence of text lines. Assume these text lines form a matrix of characters (pad the missing positions with spaces to build a rectangular matrix). Write a program to rotate the matrix by 90, 180, 270, 360, … degrees. Print the result at the console as sequence of strings.
  2.  * The first line holds a command in format "Rotate(X)" where X are the degrees of the requested rotation. The next lines contain the lines of the matrix for rotation. The input ends with the command "END". */
  3.  
  4. namespace _11.StringMatrixRotation
  5. {
  6.     using System;
  7.     using System.Collections.Generic;
  8.     using System.Text;
  9.     using System.Text.RegularExpressions;
  10.  
  11.     class StringMatrixRotation
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             string pattern = @"Rotate\((\d{1,5})\)";
  16.             string input = Console.ReadLine();
  17.  
  18.             MatchCollection matches = Regex.Matches(input, pattern);
  19.             int degrees = (int.Parse(matches[0].Groups[1].Value)) % 360;
  20.  
  21.             List<string> inputText = new List<string>();
  22.             int width = 0;
  23.             input = Console.ReadLine();
  24.             while (input != "END")
  25.             {
  26.                 inputText.Add(input);
  27.                 if (input.Length > width)
  28.                 {
  29.                     width = input.Length;
  30.                 }
  31.  
  32.                 input = Console.ReadLine();
  33.             }
  34.  
  35.             switch (degrees)
  36.             {
  37.                 case 90:
  38.                     RotateTheMatrixBy90Degrees(inputText, width);
  39.                     break;
  40.                 case 180:
  41.                     RotateTheMatrixBy180Degrees(inputText, width);
  42.                     break;
  43.                 case 270:
  44.                     RotateTheMatrixBy270Degrees(inputText, width);
  45.                     break;
  46.                 default:
  47.                     RotateTheMatrixBy0or360Degrees(inputText, width);
  48.                     break;
  49.             }
  50.         }
  51.  
  52.         private static void RotateTheMatrixBy0or360Degrees(List<string> text, int cols)
  53.         {
  54.             foreach (string currentRow in text)
  55.             {
  56.                 StringBuilder result = new StringBuilder();
  57.                 for (int col = 0; col < cols; col++)
  58.                 {
  59.                     result.Append(col < currentRow.Length ? currentRow[col] : ' ');
  60.                 }
  61.                 Console.WriteLine(result);
  62.             }
  63.         }
  64.  
  65.         private static void RotateTheMatrixBy90Degrees(List<string> text, int rows)
  66.         {
  67.             text.Reverse();
  68.             for (int row = 0; row < rows; row++)
  69.             {
  70.                 StringBuilder result = new StringBuilder();
  71.                 foreach (string t in text)
  72.                 {
  73.                     result.Append(row < t.Length ? t[row] : ' ');
  74.                 }
  75.                 Console.WriteLine(result);
  76.             }
  77.         }
  78.  
  79.         private static void RotateTheMatrixBy180Degrees(List<string> text, int cols)
  80.         {
  81.             text.Reverse();
  82.             foreach (string currentRow in text)
  83.             {
  84.                 StringBuilder result = new StringBuilder();
  85.                 for (int col = cols - 1; col >= 0; col--)
  86.                 {
  87.                     result.Append(col < currentRow.Length ? currentRow[col] : ' ');
  88.                 }
  89.                 Console.WriteLine(result);
  90.             }
  91.         }
  92.  
  93.         private static void RotateTheMatrixBy270Degrees(List<string> text, int rows)
  94.         {
  95.             for (int row = rows - 1; row >= 0; row--)
  96.             {
  97.                 StringBuilder result = new StringBuilder();
  98.                 foreach (string t in text)
  99.                 {
  100.                     result.Append(row < t.Length ? t[row] : ' ');
  101.                 }
  102.                 Console.WriteLine(result);
  103.             }
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement