Reversing directions

Chef recently printed directions from his home to a hot new restaurant across the town, but forgot to print the directions to get back home. Help Chef to transform the directions to get home from the restaurant.

A set of directions consists of several instructions. The first instruction is of the form “Begin on XXX”, indicating the street that the route begins on. Each subsequent instruction is of the form “Left on XXX” or “Right on XXX”, indicating a turn onto the specified road.

When reversing directions, all left turns become right turns and vice versa, and the order of roads and turns is reversed. See the sample input for examples.

Input

Input will begin with an integer T, the number of test cases that follow. Each test case begins with an integer N, the number of instructions in the route. N lines follow, each with exactly one instruction in the format described above.

Output

For each test case, print the directions of the reversed route, one instruction per line. Print a blank line after each test case.

Constraints

  • 1 ≤ T ≤ 15
  • 2 ≤ N ≤ 40
  • Each line in the input will contain at most 50 characters, will contain only alphanumeric characters and spaces and will not contain consecutive spaces nor trailing spaces. By alphanumeric characters we mean digits and letters of the English alphabet (lowercase and uppercase).

Sample Input

2
4
Begin on Road A
Right on Road B
Right on Road C
Left on Road D
6
Begin on Old Madras Road
Left on Domlur Flyover
Left on 100 Feet Road
Right on Sarjapur Road
Right on Hosur Road
Right on Ganapathi Temple Road

Sample Output

Begin on Road D
Right on Road C
Left on Road B
Left on Road A

Begin on Ganapathi Temple Road
Left on Hosur Road
Left on Sarjapur Road
Left on 100 Feet Road
Right on Domlur Flyover
Right on Old Madras Road

Explanation

In the first test case, the destination lies on Road D, hence the reversed route begins on Road D. The final turn in the original route is turning left from Road C onto Road D. The reverse of this, turning right from Road D onto Road C, is the first turn in the reversed route.

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader sr = new StreamReader(@"C:\Users\saikatd\input.txt");
            ArrayList directions = new ArrayList();
            ArrayList roads = new ArrayList();
            for (string str = sr.ReadLine(); str != null; str = sr.ReadLine())
            {
                Regex reg = new Regex(" on ");
                string[] tempStr = reg.Split(str);
                directions.Add(tempStr[0]);
                roads.Add(tempStr[1]);
            }
            sr.Close();
            //Cyclically rotate the directions array list
            string begin = directions[0].ToString();
            for (int i = 0; i < directions.Count - 1; i++)
            {
                directions[i] = directions[i + 1];
            }
            directions[directions.Count - 1] = begin;

            //Reversing the directions
            for (int i = directions.Count - 1; i >= 0; i--)
            {
                if (directions[i].ToString().Trim().ToLower().Contains("left"))
                {
                    Console.WriteLine("Right on " + roads[i]);
                }
                else if (directions[i].ToString().Trim().ToLower().Contains("right"))
                {
                    Console.WriteLine("Left on " + roads[i]);
                }
                else
                {
                    Console.WriteLine("Begin on " + roads[i]);
                }
            }
        }
    }
}

Input
=======
Begin on Old Madras Road
Left on Domlur Flyover
Left on 100 Feet Road
Right on Sarjapur Road
Right on Hosur Road
Right on Ganapathi Temple Road

Output
=======
Begin on Ganapathi Temple Road
Left on Hosur Road
Left on Sarjapur Road
Left on 100 Feet Road
Right on Domlur Flyover
Right on Old Madras Road
Press any key to continue . . .
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