Advertisement
dimipan80

Lego Blocks

May 7th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.27 KB | None | 0 0
  1. /* You are given two jagged arrays. Each array represents a Lego block containing integers. Your task is first to reverse the second jagged array and then check if it would fit perfectly in the first jagged array. If the arrays fit perfectly you should print out the newly made rectangular matrix. If the arrays do not match (they do not form a rectangular matrix) you should print out the number of cells in the first array and in the second array combined. */
  2.  
  3. namespace _08.LegoBlocks
  4. {
  5.     using System;
  6.     using System.Linq;
  7.  
  8.     class LegoBlocks
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             int rows = int.Parse(Console.ReadLine());
  13.             string[][] firstJagged = CreateJaggedArray(rows);
  14.             string[][] secondJagged = CreateJaggedArray(rows);
  15.  
  16.             int width = firstJagged[0].Length + secondJagged[0].Length;
  17.             bool isFit = true;
  18.             for (int i = 1; i < rows; i++)
  19.             {
  20.                 if ((firstJagged[i].Length + secondJagged[i].Length) != width)
  21.                 {
  22.                     isFit = false;
  23.                     break;
  24.                 }
  25.             }
  26.  
  27.             if (!isFit)
  28.             {
  29.                 int counter = firstJagged.Sum(arrRow => arrRow.Length);
  30.                 counter += secondJagged.Sum(arrRow => arrRow.Length);
  31.  
  32.                 Console.WriteLine("The total number of cells is: " + counter);
  33.                 return;
  34.             }
  35.  
  36.             for (int i = 0; i < rows; i++)
  37.             {
  38.                 string[] reverse = new string[secondJagged[i].Length];
  39.                 for (int j = 0; j < secondJagged[i].Length; j++)
  40.                 {
  41.                     reverse[j] = secondJagged[i][secondJagged[i].Length - j - 1];
  42.                 }
  43.  
  44.                 Console.WriteLine("[{0}, {1}]", string.Join(", ", firstJagged[i]), string.Join(", ", reverse));
  45.             }
  46.         }
  47.  
  48.         private static string[][] CreateJaggedArray(int length)
  49.         {
  50.             var jaggedArr = new string[length][];
  51.             for (int i = 0; i < jaggedArr.Length; i++)
  52.             {
  53.                 jaggedArr[i] = Console.ReadLine().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
  54.             }
  55.  
  56.             return jaggedArr;
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement