Advertisement
dimipan80

Matrix Shuffle

May 26th, 2015
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.66 KB | None | 0 0
  1. /* You are given an input string which you should fill in a square spiral matrix with a given size. After filling up the matrix you should move through it and extract from it all the letters in a chessboard pattern.
  2.  * Your next task is to check if the newly formed sentence, read in lowercase and with all non-letter characters removed, is a palindrome (reading it from left to right is the same as reading it from right to left).
  3.  * The first input line will hold a number – the size of the matrix. The second line will hold the text.
  4.  * The output should be an HTML <div> tag that shows the newly found sentence, colored by changing its background to #E0000F (see the examples below) if the sentence is not a palindrome or #4FE000 if the sentence is a palindrome. */
  5.  
  6. namespace Matrix_Shuffle
  7. {
  8.     using System;
  9.     using System.Linq;
  10.     using System.Text;
  11.  
  12.     class MatrixShuffle
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             int size = int.Parse(Console.ReadLine());
  17.             string text = Console.ReadLine();
  18.  
  19.             char[,] matrix = new char[size, size];
  20.             FillTheMatrix(matrix, text);
  21.             string extracted = ExtractLettersFromMatrixInChessboardPattern(matrix);
  22.  
  23.             Console.WriteLine(
  24.                 CheckExtractedSentenceIsPalindrome(extracted)
  25.                     ? "<div style='background-color:#4FE000'>{0}</div>"
  26.                     : "<div style='background-color:#E0000F'>{0}</div>",
  27.                 extracted);
  28.         }
  29.  
  30.         private static void FillTheMatrix(char[,] matrix, string text)
  31.         {
  32.             string direction = "right";
  33.             int size = matrix.GetLength(0);
  34.             int row = 0;
  35.             int col = 0;
  36.             for (int i = 0; i < matrix.Length && i < text.Length; i++)
  37.             {
  38.                 matrix[row, col] = text[i];
  39.                 if (direction == "right" && (col == size - 1 || matrix[row, col + 1] != '\0'))
  40.                 {
  41.                     direction = "down";
  42.                 }
  43.  
  44.                 if (direction == "down")
  45.                 {
  46.                     if (row == size - 1 || matrix[row + 1, col] != '\0')
  47.                     {
  48.                         direction = "left";
  49.                     }
  50.                     else
  51.                     {
  52.                         row++;
  53.                     }
  54.                 }
  55.  
  56.                 if (direction == "left")
  57.                 {
  58.                     if (col == 0 || matrix[row, col - 1] != '\0')
  59.                     {
  60.                         direction = "up";
  61.                     }
  62.                     else
  63.                     {
  64.                         col--;
  65.                     }
  66.                 }
  67.  
  68.                 if (direction == "up")
  69.                 {
  70.                     if (row == 0 || matrix[row - 1, col] != '\0')
  71.                     {
  72.                         direction = "right";
  73.                     }
  74.                     else
  75.                     {
  76.                         row--;
  77.                     }
  78.                 }
  79.  
  80.                 if (direction == "right")
  81.                 {
  82.                     col++;
  83.                 }
  84.             }
  85.         }
  86.  
  87.         private static string ExtractLettersFromMatrixInChessboardPattern(char[,] matrix)
  88.         {
  89.             StringBuilder firstPart = new StringBuilder();
  90.             StringBuilder secondPart = new StringBuilder();
  91.  
  92.             int size = matrix.GetLength(0);
  93.             for (int row = 0; row < size; row++)
  94.             {
  95.                 for (int col = 0; col < size; col++)
  96.                 {
  97.                     if ((row % 2 == 0 && col % 2 == 0) || (row % 2 != 0 && col % 2 != 0))
  98.                     {
  99.                         firstPart.Append(matrix[row, col]);
  100.                     }
  101.                     else
  102.                     {
  103.                         secondPart.Append(matrix[row, col]);
  104.                     }
  105.                 }
  106.             }
  107.  
  108.             return (firstPart.Append(secondPart).ToString());
  109.         }
  110.  
  111.         private static bool CheckExtractedSentenceIsPalindrome(string sentence)
  112.         {
  113.             StringBuilder sb = new StringBuilder();
  114.             foreach (char ch in sentence.Where(ch => char.IsLetter(ch)))
  115.             {
  116.                 sb.Append(char.ToLower(ch));
  117.             }
  118.  
  119.             string leftToRight = sb.ToString();
  120.             string reversed = ReverseString(leftToRight);
  121.  
  122.             return reversed == leftToRight;
  123.         }
  124.  
  125.         private static string ReverseString(string text)
  126.         {
  127.             char[] letters = text.ToCharArray();
  128.             Array.Reverse(letters);
  129.  
  130.             return new string(letters);
  131.         }
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement