Java – How do I print the coordinates of a given string?

How do I print the coordinates of a given string?… here is a solution to the problem.

How do I print the coordinates of a given string?

I

came across this question during an interview I attended, and I was confused.

Given the input string as shown below, we need to print the coordinates obtained at the last character of the string.

Route:

L-Left
U-Up
R-Right
D-Down
X-Delete the previous move.

Hypothesis:

start with the coordinates (0,0)

Here’s how to calculate the output.

Given input:

3L5UR2DDX2LR

Let’s take it step by step.

3L - Move 3 points to the left of (0,0) i.e (-3,0)

5U- Move 5 points upper to (-3,0) i.e (-3,5)

R - Move 1 point to the right of (-3,5) i.e (-2,5)

2D - Move 2 points down to (-2,5) i.e (-2,3)

D - Move 1 point further down i.e (-2,2)

x - Delete the previous move. (current value is (-2,3))

2L -Move 2 left to (-2,3) i.e(-4,3)

R- Move 1 Right to (-4,3) i.e (-3,3)

The final output is (-3,3).

I’m trying to put it into code, however, I haven’t found a starting point on how to hack it. Any help would be appreciated.

Solution

I would think of the problem as the identification of mobile instructions, each with an optional factor (how many steps) and a mandatory direction. When the factor is omitted, the value of 1 is actually depicted.

So these can be represented as patterns in regular expressions:

    String regex = "(?<factor>\\d*)"
            + "(?<dir>[LURDX])";

Once this is done, we just have to map the direction to the corresponding one
Change the coordinates (dx, dy) and then apply the instructions in the while loop that changes (multiplies the value of the factor) the regular expression match as we process the move.

Note X is a special case that can be handled by always remember
The last positions are lastX and lastY.

Here is my implementation:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Walk {
    enum Move {
        L   (-1, 0)
        , U (0, 1)
        , R (1, 0)
        , D (0, -1)
        , X (0, 0)
        ;
        private int dx;
        private int dy;
        private Move(int dx, int dy) {
            this.dx = dx;
            this.dy = dy;
        }
        public int getDx() {
            return dx;
        }
        public int getDy() {
            return dy;
        }

}

public static void main(String[] args) {
        String input = "3L5UR2DDX2LR";
        String regex = "(?<factor>\\d*)"
                + "(?<dir>[LURDX])";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(input);
        int x = 0;
        int y = 0;
        int lastX = 0;
        int lastY = 0;
        while (m.find()) {
            String factorString = m.group("factor");
            int factor;
            if (factorString.length()==0) {
                factor=1;
            } else {
                factor=Integer.parseInt(factorString);
            }
            String dirString    = m.group("dir");
            Move move = Move.valueOf(dirString);
            System.out.format("(%d,%d) last was (%d, %d) %d %s -> "
                    , x, y
                    , lastX, lastY
                    , factor, move.name());
            if (move==Move.X) {
                x = lastX;
                y = lastY;
            } else {
                lastX = x;
                lastY = y;
                x += factor * move.getDx();
                y += factor * move.getDy();
            }           
            System.out.format("(%d,%d)%n", x, y);
        }
        System.out.format("finally arrive at (%d,%d)%n", x, y);

}

}

The output of this program is this:

(0,0) last was (0, 0) 3 L -> (-3,0)
(-3,0) last was (0, 0) 5 U -> (-3,5)
(-3,5) last was (-3, 0) 1 R -> (-2,5)
(-2,5) last was (-3, 5) 2 D -> (-2,3)
(-2,3) last was (-2, 5) 1 D -> (-2,2)
(-2,2) last was (-2, 3) 1 X -> (-2,3)
(-2,3) last was (-2, 3) 2 L -> (-4,3)
(-4,3) last was (-2, 3) 1 R -> (-3,3)
finally arrive at (-3,3)

Related Problems and Solutions