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

143
Views
Algoritmo de santa claus secreto de js

Quería hacer un pequeño script en js que tenga una lista de usuarios, un usuario tiene que hacer un regalo a otro.

Aplicando las siguientes restricciones:

  1. Si "a" es el papa noel y le da un regalo a "c" no puede ser al revés. Entonces "c" no puede ser el Papá Noel de "a".

  2. Debe funcionar con un número par e impar de usuarios.

En su opinión, ¿cuál podría ser el enfoque correcto para intentar minimizar el número de comparaciones, es decir, acelerar el script?

Estaba pensando en algo como esto para empezar, pero después no estoy seguro de cómo proceder:

 let name = ['a', 'b', 'c', 'd', 'e', 'f', 'g']; let a = [...name]; let group1 = []; let groupSanta = []; let groupUser = []; for (var i = 0; i < name.length / 2 - 1; i++) { let santaClaus = a[Math.floor(Math.random() * a.length)]; a = a.filter(item => item !== santaClaus); let user = a[Math.floor(Math.random() * a.length)]; a = a.filter(item => item !== user); group1.push({ santaClaus, user }); } console.log(a, group1);

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Puede ordenar aleatoriamente la matriz y asignar a cada persona a la siguiente. Luego asigne la primera persona a la última en la matriz

 // Define names const names = ['a', 'b', 'c', 'd', 'e', 'f', 'g']; // Function to shuffle array const shuffle = (arr) => { for (let i = arr.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [arr[i], arr[j]] = [arr[j], arr[i]]; } return arr; } const randomNames = shuffle(names); // Match each person with the next one, folding over at the end const matches = randomNames.map((name, index) => { return { santa: name, receiver: randomNames[index + 1] || randomNames[0], } }); console.log(matches);

about 4 years ago · Juan Pablo Isaza Report

0

 let players = ['a', 'b', 'c', 'd', 'e', 'f', 'g']; players = shuffleArray(players) const matches = players.map((name, index) => { return { santa: name, receiver: players[index + 1] || players[0], } }); function shuffleArray(array) { let currentIndex = array.length, randomIndex while (currentIndex != 0) { randomIndex = Math.floor(Math.random() * currentIndex) currentIndex-- [array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]] } return array } console.log(matches)

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!