Sobes.tech
Middle

/** * An array of points with integer coordinates (x, y). * Determine if there exists a vertical line * that divides the points into two sets symmetric about this line. * Note: For convenience, a point can be represented not as an array [x, y], but as an object {x, y} */ isVertSym([[0, 0], [0, 0], [1, 1], [2, 2], [3, 1], [4, 0], [4, 0]]) // true isVertSym([[0, 0], [0, 0], [1, 1], [2, 2], [3, 1], [4, 0]]) // false isVertSym([]) // true isVertSym([[0, 0]]) // true isVertSym([[0, 0], [10, 0]]) // true isVertSym([[0, 0], [11, 1]]) // false isVertSym([[0, 0], [1, 0], [3, 0]]) // false function isVertSym(list) { // code here }

sobes.tech AI

Answer from AI

To check if a vertical line of symmetry exists, you can:

  1. Find the minimum and maximum x-values among the points.
  2. Calculate the potential axis of symmetry as mid = (minX + maxX) / 2.
  3. For each point, verify if there exists a mirror point with respect to this axis.

For convenience, you can use a data structure like a Map or Set to quickly check for the presence of mirror points.

Example implementation in JavaScript:

function isVertSym(points) {
  if (points.length <= 1) return true;

  let minX = Infinity, maxX = -Infinity;
  const pointSet = new Set();

  for (const [x, y] of points) {
    minX = Math.min(minX, x);
    maxX = Math.max(maxX, x);
    pointSet.add(`${x},${y}`);
  }

  const mid = (minX + maxX) / 2;

  for (const [x, y] of points) {
    const mirroredX = 2 * mid - x;
    if (!pointSet.has(`${mirroredX},${y}`)) {
      return false;
    }
  }

  return true;
}

This code checks whether, for each point, a mirror point exists with respect to the vertical line x = mid. If this condition holds for all points, the function returns true; otherwise, it returns false.

/** * An array of points with integer coordinates (x… - sobes.tech