Interview Question
Testing / Quality Assurancespublic class robotCord {
public static void main(String[] args) {
// TODO Auto-generated method stub
// int i = 2, j= 4;
// char cf = 'E';
// char[] moves = {'M','M','R','M','M','R','M','R','R','M'};
int i = 3, j= 4;
char cf = 'N';
char[] moves = {'L','M','L','M','L','M','L','M','M'};
for(int m=0; m<moves.length;m++){
if(moves[m]=='M'){
if(cf=='E'){
i++;
}
else if(cf=='W'){
i--;
}
else if(cf=='N'){
j++;
}
else if(cf=='S'){
j--;
}
if(i<0||j<0||i>6||j>7){
System.out.println("Invalid Move");
break;
}
}
else if(moves[m]=='L'){
if(cf=='E'){
cf='N';
}
else if(cf=='W'){
cf='S';
}
else if(cf=='N'){
cf='W';
}
else if(cf=='S'){
cf='E';
}
}else if(moves[m]=='R'){
if(cf=='E'){
cf='S';
}
else if(cf=='W'){
cf='N';
}
else if(cf=='N'){
cf='E';
}
else if(cf=='S'){
cf='W';
}
}
}
System.out.println("Results: "+i+" "+j+" "+cf);
}
}
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class PointMove
{
public static char curDirection(char a, char b){
char newDirection=a;
switch(a){
case 'N': if (b == 'L'){
newDirection = 'W';
}else{
newDirection = 'E';
}
break;
case 'E': if (b == 'L'){
newDirection = 'N';
}else{
newDirection = 'S';
}
break;
case 'S': if (b == 'L'){
newDirection = 'E';
}else{
newDirection = 'W';
}
break;
case 'W': if (b == 'L'){
newDirection = 'S';
}else{
newDirection = 'N';
}
break;
}
return newDirection;
}
public static int move (int a, char b,int limit){
if (b =='N'){
return (a+1)%limit;
}
if ( b == 'E'){
return (a+1)%limit;
}
if (b =='W'){
return (a-1)%limit;
}
else{
return (a-1)%limit;
}
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int minX=0,minY=0;
Scanner sc = new Scanner(System.in);
int maxX= sc.nextInt();
int maxY= sc.nextInt();
char curmove;
int currX= sc.nextInt();
int currY=sc.nextInt();
char currentDirection= sc.nextLine().charAt(1);
String moves= sc.nextLine();
for(int i =0; i< moves.length(); i++){
curmove=moves.charAt(i);
if (curmove == 'M'){
if (currentDirection == 'E' || currentDirection =='W'){
currX=move(currX,currentDirection,maxX);
}else{
currY=move(currY,currentDirection,maxY);
}
}
else{
currentDirection=curDirection(currentDirection,curmove);
}
}
//System.out.println(currentDirection);;
//curDirection('N','L');
System.out.println(currX+" "+currY+ " "+ currentDirection);
}
}
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class PointMove
{
public static char curDirection(char a, char b){
char newDirection=a;
switch(a){
case 'N': if (b == 'L'){
newDirection = 'W';
}else{
newDirection = 'E';
}
break;
case 'E': if (b == 'L'){
newDirection = 'N';
}else{
newDirection = 'S';
}
break;
case 'S': if (b == 'L'){
newDirection = 'E';
}else{
newDirection = 'W';
}
break;
case 'W': if (b == 'L'){
newDirection = 'S';
}else{
newDirection = 'N';
}
break;
}
return newDirection;
}
public static int move (int a, char b,int limit){
if (b =='N'){
return (a+1)%limit;
}
if ( b == 'E'){
return (a+1)%limit;
}
if (b =='W'){
return (a-1)%limit;
}
else{
return (a-1)%limit;
}
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int minX=0,minY=0;
Scanner sc = new Scanner(System.in);
int maxX= sc.nextInt();
int maxY= sc.nextInt();
char curmove;
int currX= sc.nextInt();
int currY=sc.nextInt();
char currentDirection= sc.nextLine().charAt(1);
String moves= sc.nextLine();
for(int i =0; i< moves.length(); i++){
curmove=moves.charAt(i);
if (curmove == 'M'){
if (currentDirection == 'E' || currentDirection =='W'){
currX=move(currX,currentDirection,maxX);
}else{
currY=move(currY,currentDirection,maxY);
}
}
else{
currentDirection=curDirection(currentDirection,curmove);
}
}
//System.out.println(currentDirection);;
//curDirection('N','L');
System.out.println(currX+" "+currY+ " "+ currentDirection);
}
}
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class PointMove
{
public static char curDirection(char a, char b){
char newDirection=a;
switch(a){
case 'N': if (b == 'L'){
newDirection = 'W';
}else{
newDirection = 'E';
}
break;
case 'E': if (b == 'L'){
newDirection = 'N';
}else{
newDirection = 'S';
}
break;
case 'S': if (b == 'L'){
newDirection = 'E';
}else{
newDirection = 'W';
}
break;
case 'W': if (b == 'L'){
newDirection = 'S';
}else{
newDirection = 'N';
}
break;
}
return newDirection;
}
public static int move (int a, char b,int limit){
if (b =='N'){
return (a+1)%limit;
}
if ( b == 'E'){
return (a+1)%limit;
}
if (b =='W'){
return (a-1)%limit;
}
else{
return (a-1)%limit;
}
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int minX=0,minY=0;
Scanner sc = new Scanner(System.in);
int maxX= sc.nextInt();
int maxY= sc.nextInt();
char curmove;
int currX= sc.nextInt();
int currY=sc.nextInt();
char currentDirection= sc.nextLine().charAt(1);
String moves= sc.nextLine();
for(int i =0; i< moves.length(); i++){
curmove=moves.charAt(i);
if (curmove == 'M'){
if (currentDirection == 'E' || currentDirection =='W'){
currX=move(currX,currentDirection,maxX);
}else{
currY=move(currY,currentDirection,maxY);
}
}
else{
currentDirection=curDirection(currentDirection,curmove);
}
}
//System.out.println(currentDirection);;
//curDirection('N','L');
System.out.println(currX+" "+currY+ " "+ currentDirection);
}
}
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class PointMove
{
public static char curDirection(char a, char b){
char newDirection=a;
switch(a){
case 'N': if (b == 'L'){
newDirection = 'W';
}else{
newDirection = 'E';
}
break;
case 'E': if (b == 'L'){
newDirection = 'N';
}else{
newDirection = 'S';
}
break;
case 'S': if (b == 'L'){
newDirection = 'E';
}else{
newDirection = 'W';
}
break;
case 'W': if (b == 'L'){
newDirection = 'S';
}else{
newDirection = 'N';
}
break;
}
return newDirection;
}
public static int move (int a, char b,int limit){
if (b =='N'){
return (a+1)%limit;
}
if ( b == 'E'){
return (a+1)%limit;
}
if (b =='W'){
return (a-1)%limit;
}
else{
return (a-1)%limit;
}
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int minX=0,minY=0;
Scanner sc = new Scanner(System.in);
int maxX= sc.nextInt();
int maxY= sc.nextInt();
char curmove;
int currX= sc.nextInt();
int currY=sc.nextInt();
char currentDirection= sc.nextLine().charAt(1);
String moves= sc.nextLine();
for(int i =0; i< moves.length(); i++){
curmove=moves.charAt(i);
if (curmove == 'M'){
if (currentDirection == 'E' || currentDirection =='W'){
currX=move(currX,currentDirection,maxX);
}else{
currY=move(currY,currentDirection,maxY);
}
}
else{
currentDirection=curDirection(currentDirection,curmove);
}
}
//System.out.println(currentDirection);;
//curDirection('N','L');
System.out.println(currX+" "+currY+ " "+ currentDirection);
}
}
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class PointMove
{
public static char curDirection(char a, char b){
char newDirection=a;
switch(a){
case 'N': if (b == 'L'){
newDirection = 'W';
}else{
newDirection = 'E';
}
break;
case 'E': if (b == 'L'){
newDirection = 'N';
}else{
newDirection = 'S';
}
break;
case 'S': if (b == 'L'){
newDirection = 'E';
}else{
newDirection = 'W';
}
break;
case 'W': if (b == 'L'){
newDirection = 'S';
}else{
newDirection = 'N';
}
break;
}
return newDirection;
}
public static int move (int a, char b,int limit){
if (b =='N'){
return (a+1)%limit;
}
if ( b == 'E'){
return (a+1)%limit;
}
if (b =='W'){
return (a-1)%limit;
}
else{
return (a-1)%limit;
}
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int minX=0,minY=0;
Scanner sc = new Scanner(System.in);
int maxX= sc.nextInt();
int maxY= sc.nextInt();
char curmove;
int currX= sc.nextInt();
int currY=sc.nextInt();
char currentDirection= sc.nextLine().charAt(1);
String moves= sc.nextLine();
for(int i =0; i< moves.length(); i++){
curmove=moves.charAt(i);
if (curmove == 'M'){
if (currentDirection == 'E' || currentDirection =='W'){
currX=move(currX,currentDirection,maxX);
}else{
currY=move(currY,currentDirection,maxY);
}
}
else{
currentDirection=curDirection(currentDirection,curmove);
}
}
//System.out.println(currentDirection);;
//curDirection('N','L');
System.out.println(currX+" "+currY+ " "+ currentDirection);
}
}
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class PointMove
{
public static char curDirection(char a, char b){
char newDirection=a;
switch(a){
case 'N': if (b == 'L'){
newDirection = 'W';
}else{
newDirection = 'E';
}
break;
case 'E': if (b == 'L'){
newDirection = 'N';
}else{
newDirection = 'S';
}
break;
case 'S': if (b == 'L'){
newDirection = 'E';
}else{
newDirection = 'W';
}
break;
case 'W': if (b == 'L'){
newDirection = 'S';
}else{
newDirection = 'N';
}
break;
}
return newDirection;
}
public static int move (int a, char b,int limit){
if (b =='N'){
return (a+1)%limit;
}
if ( b == 'E'){
return (a+1)%limit;
}
if (b =='W'){
return (a-1)%limit;
}
else{
return (a-1)%limit;
}
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int minX=0,minY=0;
Scanner sc = new Scanner(System.in);
int maxX= sc.nextInt();
int maxY= sc.nextInt();
char curmove;
int currX= sc.nextInt();
int currY=sc.nextInt();
char currentDirection= sc.nextLine().charAt(1);
String moves= sc.nextLine();
for(int i =0; i< moves.length(); i++){
curmove=moves.charAt(i);
if (curmove == 'M'){
if (currentDirection == 'E' || currentDirection =='W'){
currX=move(currX,currentDirection,maxX);
}else{
currY=move(currY,currentDirection,maxY);
}
}
else{
currentDirection=curDirection(currentDirection,curmove);
}
}
//System.out.println(currentDirection);;
//curDirection('N','L');
System.out.println(currX+" "+currY+ " "+ currentDirection);
}
}
public class robotCord {
- Srinidhi June 10, 2015public static void main(String[] args) {
// TODO Auto-generated method stub
// int i = 2, j= 4;
// char cf = 'E';
// char[] moves = {'M','M','R','M','M','R','M','R','R','M'};
int i = 3, j= 4;
char cf = 'N';
char[] moves = {'L','M','L','M','L','M','L','M','M'};
for(int m=0; m<moves.length;m++){
if(moves[m]=='M'){
if(cf=='E'){
i++;
}
else if(cf=='W'){
i--;
}
else if(cf=='N'){
j++;
}
else if(cf=='S'){
j--;
}
if(i<0||j<0||i>6||j>7){
System.out.println("Invalid Move");
break;
}
}
else if(moves[m]=='L'){
if(cf=='E'){
cf='N';
}
else if(cf=='W'){
cf='S';
}
else if(cf=='N'){
cf='W';
}
else if(cf=='S'){
cf='E';
}
}else if(moves[m]=='R'){
if(cf=='E'){
cf='S';
}
else if(cf=='W'){
cf='N';
}
else if(cf=='N'){
cf='E';
}
else if(cf=='S'){
cf='W';
}
}
}
System.out.println("Results: "+i+" "+j+" "+cf);
}
}