Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

80
Views
¿Cómo puedo obtener una secuencia de dos elementos de matriz en una matriz?

estoy buscando una manera de obtener una secuencia de dos elementos de matriz en una matriz, especialmente una matriz de longitud 4

por ejemplo,

 [[1,0], [2,3], [5,4], [0,0], [3,2], [1,4], [0,5]]

... debe devolver:

 [[3,2], [2,3], [1,4], [0,5]]

3 --^ 2 -----^ 1 ----^ 0 -----^ entonces [3, 2, 1, 0] para x

 [[3,2], [2,3], [1,4], [0,5]]

2 -----^ 3 -----^ 4 ----^ 5 -----^ entonces [2, 3, 4, 5] para y

 [[x1, y1], [x2, y2], [x3, y3], [x4, y3]] // +1 or -1 for the first index // and +1 or -1 for the second index [[3,2], [2,3], [1,4], [0,5]] // is a sequence [[0,0], [1,1], [2,2], [3,3]] // is a sequence [[4,4], [3,3], [2,2], [1,1]] // is a sequence [[4,3], [3,3], [2,2], [1,1]] // is not a sequence [[1,2], [2,3], [4,5], [5,6]] // is a sequence

Traté de usar for loops pero es ilegible y confuso, tal vez demasiado difícil y esto, pero solo cuenta la secuencia más larga, no la devuelve:

 const Z = x.sort((a, b) => a - b).reduce((count, val, i) => { return count += val + 1 === x[i + 1] ? 1 : 0 }, 1); const Z2 = y.sort((a, b) => a - b).reduce((count, val, i) => { return count += val + 1 === y[i + 1] ? 1 : 0 }, 1); console.log(Z, Z2) // 4 4
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede agregar los puntos a un objeto y verificar el orden de cuatro usando un factor de agregar un desplazamiento para x e y .

Si es necesario, también puede agregar la verificación de puntos horizontales o verticales.

 const four = array => { const data = array.reduce((r, [x, y]) => ((r[x] ??= {})[y] = true, r), {}), check = ([x, y], i, j) => { const temp = []; for (let k = 0; k < 4; k++, x += i, y += j) { if (data[x]?.[y]) temp.push([x, y]); else break; } if (temp.length === 4) return temp; }; let result; array.some(p => result = check(p, 1, 1) || check(p, -1, 1)); return result; }, data = [[1, 0], [2, 3], [5, 4], [0, 0], [3, 2], [1, 4], [0, 5]]; console.log(four(data)); // [[3, 2], [2, 3], [1, 4], [0, 5]]
 .as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!