skip to Main Content

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


  1. chechk this :

    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");
          let leftTurns = 0;
          let rightTurns = 0;
          
          for (let i = 0; i < moves.length; i++) {
            let movement = moves[i];
            if (movement === "<") {
              xPos--;
              xMin = Math.min(xMin, xPos);
              leftTurns++;
            } else if (movement === ">") {
              xPos++;
              xMax = Math.max(xMax, xPos);
              rightTurns++;
            } 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;
          
          if (width > height || height > width) return false;
          if (visited.size !== (width + 1) * (height + 1)) return false;
          if (leftTurns !== rightTurns) return false;
          
          return true;
        }
    
    Login or Signup to reply.
  2. 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:

    • All four symbols appear, and are contiguous (with wrap-around).
    • You have equal counts of opposite symbols
    • Opposites aren’t adjacent.

    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:

    • Count the contiguous occurrences of the e1, call this k1.
    • Return False if the e2 doesn’t exist or is the opposite e1.
    • Count the contiguous occurrences of e2, call this k2.
    • Return False if e3 doesn’t exist or isn’t the opposite of e1.
    • Count the contiguous occurrences of e3, and return False unless this equals k1.
    • Return False if e4 doesn’t exist or isn’t the opposite of e2.
    • Count the contiguous occurrences of e4, and return False unless this equals k1.
    • Return False if you haven’t reached the end of the string.
    • Return True.
    Login or Signup to reply.
  3. I see a few things here.

    1. To be a rectangle, the number of "<" and ">" must match, as well as the "^" and "v".
    2. All transitions must be from ("<" or ">") to ("^" or "v") OR ("^" or "v") to ("<" or ">")
    3. The number of transitions must be 3, but can also be 4 if the first and last directions match.

    I think your code may handle #1, maybe your validation handles #2.
    For #3, try this:

    // Assumes that #1 above has already been confirmed

    const moves='^^^^^^<<<<vv>>v<<vvv>>>>';//"^^^<<<<vvv>>>>";
        function testit() {
            alert(solution(moves));
            alert(checkTransitionCount(moves));
        }
    //function solution(moves){...this is your code...}
    
    function checkTransitionCount(moves) {
      let transitionCount = 0;
      let nextChar = moves[0];
      for (let idx = 1; idx < moves.length; idx++) {
        if (nextChar != moves[idx]) {
          transitionCount++;
          nextChar = moves[idx];
        }
      }
      if (transitionCount == 3)
        return true;
      if (transitionCount  == 4)
        if (moves[0] == moves[moves.length - 1])
          return true;
    
      return false;
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search