-1

A robot lands on Mars, which happens to be a cartesian grid; assuming that we hand the robot these instructions, such as LFFFRFFFRRFFF, where "L" is a "turn 90 degrees left", "R" is a "turn 90 degrees right", and "F" is "go forward one space, please write control code for the robot such that it ends up at the appropriate-and-correct destination, and include unit tests.

Here is an example output with command "FF":

[0, 2]

I could find solution to this problem on Google, but I am not very clear with the explanation. I am trying to clearly understand the logic on how to solve this and implement in Java. Any help is appreciated.

Update: This is an interview question. For now, I trying improve my knowledge. It is interesting question to me. I can remove if i am breaking any stack over rule.

Here is the research I did: Is there an equivalent of Scala's Either in Java 8?

Algorithm for finding all paths in a NxN grid

http://www.geeksforgeeks.org/check-if-a-given-sequence-of-moves-for-a-robot-is-circular-or-not/

Community
  • 1
  • 1
N..
  • 906
  • 7
  • 23
  • @SuperCoolHandsomeGelBoy If you read the task three or more times, you would get the surprizing answer: Yes, it is and it is obvious. – Nikolas Charalambidis May 26 '16 at 17:25
  • I hate to give hints on this if this interview question is TO BE ANSWERED. –  May 26 '16 at 17:26
  • Tell me reason why you vote -1. I can remove question. I am trying to find answer for everyone. – N.. May 26 '16 at 17:27
  • @SuperCoolHandsomeGelBoy I already said to employer I am not able to solve this else upwork can help to get this but I am trying to know . I am not try to cheat and get job. that's something I don't do and will never do. – N.. May 26 '16 at 17:28
  • @BostonStar Which particular part do you have trouble understanding? Draw a normal X, Y coordinate system, X-axis going right, Y-axis going up. Place robot at `0,0` and make it face up. `F` moves forward 1, leaving robot at `0,1`. Another `F` moves forward 1, leaving robot at `0,2`. `L` turns robot left. `F` moves forward 1, leaving robot at `-1,2`. And so on. Now, starting at `0,0`, where does `LFFFRFFFRRFFF` leave the robot? – Andreas May 26 '16 at 17:30
  • To follow I think aswer is [3,6] ? – N.. May 26 '16 at 17:39

3 Answers3

1
import java.util.Scanner;

public class Run{
    public static void main(String []args){
        int angle = 0, c = 0;
        int[] coords = {0,0};
        String d;
        Scanner in = new Scanner(System.in);
        System.out.println("Input your command");
        d = in.nextLine();
        for (c = 0; c < d.length; c++){
            if (d[c] == 'R'){
                 angle = (angle + 90)%360;
            }else if (d[c] == 'L'){
                 angle = (angle + 270)%360;
            }else if (d[c] == 'F'){
                 switch(angle){
                      case 0: coords[1]++;break;
                      case 90: coords[0]++;break;
                      case 180: coords[1]--;break;
                      case 270: coords[0]--;
                }
            }
         }
         System.out.println('['+coords[0]+','+coords[1]+']');
    }
}

I think this code is simple enough. Currently I have no Java environment, so I cannot test it. Correct me if I am wrong

1

I have prepared for you the following code:

public static String calculate(String str) {
    int x = 0;
    int y = 0;
    int[][] move = {{0,1}, {1,0}, {-1,0}, {0,-1}};
    int dir = 0;

    for (char ch: str.toCharArray()) {
        if (ch == 'F') { 
            x += move[dir][0]; 
            y += move[dir][1]; 
        } else if (ch == 'L') {
            dir++;
        } else if (ch == 'R') {
            dir--;
        }
        dir = (dir + 4) % 4;
    }
 return "[" + x + ", " + y + "]";
 }

Let's analyse my naïve solution.

  • int[][] move = {{0,1}, {1,0}, {-1,0}, {0,-1}}; is all the combinations of movement when you rotate to left led with variable dir.
  • Loop the String str to get the each command
  • Read the command and in case of F, move torwards in the direction dir. Subarray is for the axis.
  • In case of rotating, add or subtract the dir for the correct result.
  • Finally make the dir to not overflow with dir = (dir + 4) % 4;. It means when I move down dir = 0 and I go left (dir--), it will lead dir=-1, that is illegal. So dir + 4 = 3 and 3 % 4 = 3, it gives the last set of direction.

Hope it helps and you understand now the logics.

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
0

Super Cool handsome,your logic is correct based on my opinion, I tested your code , i found some errors, your string d need to convert array before for loop *

in.nextLine().toCharArray()

*; and string d will be char[]d . Also your print output i have changed

("["+coords[0]+","+coords[1]+"]")

. here is my full code which I did.After that it was working

Sharif
  • 1