Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 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.
- * 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).
- * The first input line will hold a number – the size of the matrix. The second line will hold the text.
- * 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. */
- namespace Matrix_Shuffle
- {
- using System;
- using System.Linq;
- using System.Text;
- class MatrixShuffle
- {
- static void Main(string[] args)
- {
- int size = int.Parse(Console.ReadLine());
- string text = Console.ReadLine();
- char[,] matrix = new char[size, size];
- FillTheMatrix(matrix, text);
- string extracted = ExtractLettersFromMatrixInChessboardPattern(matrix);
- Console.WriteLine(
- CheckExtractedSentenceIsPalindrome(extracted)
- ? "<div style='background-color:#4FE000'>{0}</div>"
- : "<div style='background-color:#E0000F'>{0}</div>",
- extracted);
- }
- private static void FillTheMatrix(char[,] matrix, string text)
- {
- string direction = "right";
- int size = matrix.GetLength(0);
- int row = 0;
- int col = 0;
- for (int i = 0; i < matrix.Length && i < text.Length; i++)
- {
- matrix[row, col] = text[i];
- if (direction == "right" && (col == size - 1 || matrix[row, col + 1] != '\0'))
- {
- direction = "down";
- }
- if (direction == "down")
- {
- if (row == size - 1 || matrix[row + 1, col] != '\0')
- {
- direction = "left";
- }
- else
- {
- row++;
- }
- }
- if (direction == "left")
- {
- if (col == 0 || matrix[row, col - 1] != '\0')
- {
- direction = "up";
- }
- else
- {
- col--;
- }
- }
- if (direction == "up")
- {
- if (row == 0 || matrix[row - 1, col] != '\0')
- {
- direction = "right";
- }
- else
- {
- row--;
- }
- }
- if (direction == "right")
- {
- col++;
- }
- }
- }
- private static string ExtractLettersFromMatrixInChessboardPattern(char[,] matrix)
- {
- StringBuilder firstPart = new StringBuilder();
- StringBuilder secondPart = new StringBuilder();
- int size = matrix.GetLength(0);
- for (int row = 0; row < size; row++)
- {
- for (int col = 0; col < size; col++)
- {
- if ((row % 2 == 0 && col % 2 == 0) || (row % 2 != 0 && col % 2 != 0))
- {
- firstPart.Append(matrix[row, col]);
- }
- else
- {
- secondPart.Append(matrix[row, col]);
- }
- }
- }
- return (firstPart.Append(secondPart).ToString());
- }
- private static bool CheckExtractedSentenceIsPalindrome(string sentence)
- {
- StringBuilder sb = new StringBuilder();
- foreach (char ch in sentence.Where(ch => char.IsLetter(ch)))
- {
- sb.Append(char.ToLower(ch));
- }
- string leftToRight = sb.ToString();
- string reversed = ReverseString(leftToRight);
- return reversed == leftToRight;
- }
- private static string ReverseString(string text)
- {
- char[] letters = text.ToCharArray();
- Array.Reverse(letters);
- return new string(letters);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement