Chatruka’s Parallelogram

Raja Chatruka needs a message to be delivered to his spy. The message is supposed to be encoded so that none other than the spy can decode the message, who knows the process of encoding.

 

Earlier the Raja and the Spy both decided to adopt the process of “expanding parallelogram code”. The method encodes the message by placing the characters in an odd order parallelogram matrix and then reading it in a clockwise spiral from the centre. Each cell of the parallelogram contains two characters, the one below is filled first as well as read first. The length of message determines the order of the matrix. The order needs to be minimum. If the number of characters are less than the number of cells in the matrix the remaining cells are filled with “/”. No message contains the actual character /.

For example: If the message is “this is a problem”

 

The output should be “a s this iprm/leob”

Your program must be able to encode and decode the message by this process.

INPUT

The input will consist of a pair of lines. The first line will contain either of the two words “ENCODE” or “DECODE” in uppercase letters. The second line will contain the message(less than 100 characters) to be encoded or decoded depending upon the first line.

 

OUTPUT

The output will consist of three lines. The first line will print “OUTPUT IS” in uppercase letters. The second line will print the original message, and the third line the resultant message. The character ‘/’ should be removed to get the decoded message.

Note: Order of a matrix refers to the number of rows or columns.

SAMPLE INPUT:

ENCODE

this is a problem

 

SAMPLE OUTPUT:

OUTPUT IS

this is a problem

a s this iprm/leob

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            //Chatruka’s Parallelogram
            string message = "This is a problem.";
            Console.WriteLine("Original Message: " + message);
            Console.WriteLine("Encoded Message: " + EncodeMessage(message));
            Console.WriteLine("Decoded Message: " + DecodeMessage(EncodeMessage(message)));
        }
        public static int GetMatrixOrder(string message)
        {
            int i = 0;
            for (i = 1; (int)Math.Pow(i, 2) * 2 < message.Length; i += 2)
            {
                //do nothing
            }
            return (i* 2);
        }
        public static string EncodeMessage(string message)
        {
            int matrixOrder = GetMatrixOrder(message);
            char[,] messageMatrix = new char[matrixOrder / 2, matrixOrder];
            int counter = 0;
            //Fill message matrix
            for (int i = 0; i < messageMatrix.GetLength(0); i++)
            {
                for (int j = 0; j < messageMatrix.GetLength(1); j++)
                {
                    if (counter < message.Length)
                    {
                        messageMatrix[i, j] = message[counter];
                        counter++;
                    }
                    else
                    {
                        messageMatrix[i, j] = ‘/’;
                    }
                }
            }
            string encodedMessage = "";
            int startRow = messageMatrix.GetLength(0) / 2;
            int startCol = messageMatrix.GetLength(1) / 2 – 1;
            int elementCount = 0;
            encodedMessage = encodedMessage + messageMatrix[startRow, startCol].ToString() + messageMatrix[startRow, startCol + 1].ToString();
            elementCount += 2;
            startCol -= 2;
            int spiralStep = 1;
            while (elementCount < (messageMatrix.GetLength(0) * messageMatrix.GetLength(1)))
            {
                //Move Up
                for (int i = 0; i < spiralStep + 1; i++)
                {
                    encodedMessage = encodedMessage + messageMatrix[startRow, startCol].ToString() + messageMatrix[startRow, startCol + 1].ToString();
                    startRow–;
                    elementCount += 2;
                }
                startRow++;
                startCol += 2;
                //Move Right
                for (int i = 0; i < spiralStep + 1; i++)
                {
                    encodedMessage = encodedMessage + messageMatrix[startRow, startCol].ToString() + messageMatrix[startRow, startCol + 1].ToString();
                    startCol += 2;
                    elementCount += 2;
                }
                startCol -= 2;
                startRow++;
                //Move Down
                for (int i = 0; i < spiralStep + 1; i++)
                {
                    encodedMessage = encodedMessage + messageMatrix[startRow, startCol].ToString() + messageMatrix[startRow, startCol + 1].ToString();
                    startRow++;
                    elementCount += 2;
                }
                startRow–;
                startCol -= 2;
                //Move Left
                for (int i = 0; i < spiralStep + 1; i++)
                {
                    encodedMessage = encodedMessage + messageMatrix[startRow, startCol].ToString() + messageMatrix[startRow, startCol + 1].ToString();
                    startCol -= 2;
                    elementCount += 2;
                }
                spiralStep += 2;
            }
            return encodedMessage;
        }
        public static string DecodeMessage(string message)
        {
            int matrixOrder = (int)Math.Sqrt((message.Length) / 2);
            char[,] messageMatrix = new char[matrixOrder, matrixOrder * 2];
            string decodedMessage = "";
            //Filling the message matrix from center
            int startRow = messageMatrix.GetLength(0) / 2;
            int startCol = messageMatrix.GetLength(1) / 2 – 1;
            int elementCount = 0;
            messageMatrix[startRow, startCol] = message[elementCount];
            messageMatrix[startRow, startCol + 1] = message[elementCount + 1];
            elementCount += 2;
            startCol -= 2;
            int spiralStep = 1;
            //Modify
            while (elementCount < message.Length)
            {
                //Move Up
                for (int i = 0; i < spiralStep + 1; i++)
                {
                    messageMatrix[startRow, startCol] = message[elementCount];
                    messageMatrix[startRow, startCol + 1] = message[elementCount + 1];
                    startRow–;
                    elementCount += 2;
                }
                startRow++;
                startCol += 2;
                //Move Right
                for (int i = 0; i < spiralStep + 1; i++)
                {
                    messageMatrix[startRow, startCol] = message[elementCount];
                    messageMatrix[startRow, startCol + 1] = message[elementCount + 1];
                    startCol += 2;
                    elementCount += 2;
                }
                startCol -= 2;
                startRow++;
                //Move Down
                for (int i = 0; i < spiralStep + 1; i++)
                {
                    messageMatrix[startRow, startCol] = message[elementCount];
                    messageMatrix[startRow, startCol + 1] = message[elementCount + 1];
                    startRow++;
                    elementCount += 2;
                }
                startRow–;
                startCol -= 2;
                //Move Left
                for (int i = 0; i < spiralStep + 1; i++)
                {
                    messageMatrix[startRow, startCol] = message[elementCount];
                    messageMatrix[startRow, startCol + 1] = message[elementCount + 1];
                    startCol -= 2;
                    elementCount += 2;
                }
                spiralStep += 2;
            }
            int flag = 0;
            for (int i = 0; i < messageMatrix.GetLength(0); i++)
            {
                for (int j = 0; j < messageMatrix.GetLength(1); j++)
                {
                    if (messageMatrix[i, j] == ‘/’)
                    {
                        flag = 1;
                        break;
                    }
                    decodedMessage = decodedMessage + messageMatrix[i, j];
                }
                if (flag == 1)
                {
                    break;
                }
            }
            return decodedMessage;
        }
    }
}

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s