Telerik Academy Practical Exam – Problem 4 – “Carpets”

So the practical exam in the Telerik Academy passed and I decided to share one of the most troublesome problem for me there. A problem that took me 2 hours to solve. Here is the condition of the task :

Telerik Academy is considering opening a new office in Great Britain. Therefore the whole Trainers team is traveling to the United Kingdom for the important event. Of course all of them want to feel exactly like home in the new office, so they ordered some special carpets from Chiprovtsi. Those carpets consist of many embedded rhombs. Please help them and print some carpets in different sizes for the new Telerik Academy Head Quarters.

Input

Integer number N [6;80]

Output Example:

sample
So here is my solution:

namespace Problem_4
{
    class Carpets
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            int dots = n / 2 - 1;
            string leftside = "/";          //variable which will print the "/"
            string rightside = "\\";        //variable which will print the "\"
            int stringlength;
            // We print the 1st line cause it will be the same for every N
            Console.Write(new string('.', dots) + leftside + new string(' ', 0) + rightside + new string('.', dots));
            Console.WriteLine();
            dots--;
            // Then we do a for loop for half of the Rhomb

            for (int i =2; i <= n/2; i++)
            {
// For each even row we add 2 spaces
                 if (i % 2 == 0)
                 {
                 Console.Write(new string('.', dots) + leftside + new string(' ', 2) + rightside + new str                 ing('.', dots));
                 Console.WriteLine();
                 dots--;
                 }
//For each odd row which will be the start of a new rhomb we don't add spaces
                 else
                 {
                 leftside = leftside + " /"; // and we add " /" to the left side
                 rightside = rightside + " \\"; //and " \" to the right side
                 Console.Write(new string('.', dots) + leftside + rightside + new string('.', dots));
                 Console.WriteLine();
                 dots--;
                 }
            }
dots = 0;
        //Analogical to the first loop we do the same but in reverse for the second part of the Rhomb
              for (int i = n/2; i >=2; i--)
              {
                   if (i % 2 == 0)
                   {
                    Console.Write(new string('.', dots) + rightside + new string(' ', 2) + leftside + new string('.', dots));
                    Console.WriteLine();
                    dots++;
                    }
                   else
                    {

                   Console.Write(new string('.', dots) + rightside + leftside + new string('.', dots));
                   Console.WriteLine();
                   dots++;
       // here we get the lenght of the string and remove the last 2 symbols " /"
                  stringlength = leftside.Length;
                  leftside = leftside.Remove(stringlength -2, 2);

        // we do the same for the other string
                 stringlength = rightside.Length;
                 rightside = rightside.Remove(stringlength - 2, 2);
                     }
               }
     //printing the last row of the rhomb again
  Console.Write(new string('.', dots) + rightside + new string(' ',0) + leftside + new string('.', dots));
       }

   }
}

And the final result is beautiful:

2 responses to “Telerik Academy Practical Exam – Problem 4 – “Carpets”

  1. Така тръгнах и аз, но не се получи няколко пъти и тогава прибягнах до матрицата. Браво, че не си се отказал от решение без масиви!

Leave a comment