A drawing robot is positioned at point(0,0) of a coordinate plane, and the string moves describes the path the robot
will take.
Each of the strings’s characters describes a single move made by the robot. Moreover, the robot will make the moves
in the exacat same order as they appear in the string, starting from position (0.0). Assuming the robot has already
made somes moves from the string, and is currently at point(x,y), if the next character of the string is:
"^":the robot will move to (x,y+1),
"v":the robot will move to(x,y-1),
"<":the robot will move to (x -1,y),
">":the robotwill move to (x+1,y).
Each time the robot moves, it draws a live between its current position and the point to which
it moves. It is guaranteed that the instructions provided in the string moves will never cause the robot to visit the
same point more than once, with the exception of point(0,0), which might be visited by the robot exactly twice: at the
beginning and at the end of the robot’s path. The task is to determine wether.
After the robot has made all moves provided in the string, all the lines it has drawn form a single rectangle.
Write a function: function solution(moves): that, given the string moves, returns true if the robot’s path will
form a rectangle or false otherwise.
Given moves="^^^<<<<vvv>>>",
the function should return true.
Gives moves="<vvv>>^^^<"
, the function should return true.
Assume that: the length of the string moves is within range [1…100].
String moves is made only of the following
characters:"^", "v", "<" and/or ">".
The robot never visits the same spot twice, except for point(0,0), which may
be visited at the start and end of the robot’s path.
This is the code i wrote, it works only to check if the shape is a square or a rectangle,
but it cant tell if the shape has more turns while still having height and width be different,
for example, moves="^^^^^^<<<<vv>>v<<vvv>>>>";
will not make a rectangle, but because it has the
same height on both sides it will count as rectangle.
const moves = "^^^<<<<vvv>>>>";
console.log(solution(moves));
function solution(moves) {
let xPos = 0,
yPos = 0;
let xMin = 0,
xMax = 0,
yMin = 0,
yMax = 0;
let visited = new Set();
visited.add("0,0");
for (let i = 0; i < moves.length; i++) {
let movement = moves[i];
if (movement === "<") {
xPos--;
xMin = Math.min(xMin, xPos);
} else if (movement === ">") {
xPos++;
xMax = Math.max(xMax, xPos);
} else if (movement === "^") {
yPos++;
yMax = Math.max(yMax, yPos);
} else if (movement === "v") {
yPos--;
yMin = Math.min(yMin, yPos);
}
let currPos = `${xPos},${yPos}`;
visited.add(currPos);
}
if (xPos !== 0 || yPos !== 0) return false;
const width = xMax - xMin + 1;
const height = yMax - yMin + 1;
console.log(visited);
if (width > height || height > width) return true;
else return false;
}
I want to know what operation would i need to ensure it will return true only if a rectangle is formed.
3
Answers
chechk this :
An empty string corresponds to a point which could be regarded as a zero-area rectangle. Other than that, the input defines a rectangle if and only if:
To avoid having to worry about wrap around, shift the input string so the first elt of the shifted string is the second distinct elt of the input.
I’ll use e1, e2, e3, and e4 to refer to the first, second, third, and fourth distinct element you encounter in the shifted string.
Then with your shifted string:
I see a few things here.
I think your code may handle #1, maybe your validation handles #2.
For #3, try this:
// Assumes that #1 above has already been confirmed