Home > Software design >  Js secret santa claus algoritm
Js secret santa claus algoritm

Time:12-08

I wanted to make a small script in js that having a list of users, one user has to make a gift to another.

By applying the following constraints:

  1. If "a" is the santa claus and gives a gift to "c" it cannot be the other way around. So "c" cannot be the santa claus of "a".

  2. It must work with both an even and an odd number of users.

In your opinion, what could be the right approach to use to try to minimize the number of comparisons, that is, speed up the script.

I was thinking something like this to start, but afterwards I'm not sure how to proceed:

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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You can just randomly sort the array and assign each person to the next one. Then assign the first person to the last one in the array

// 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);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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)
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related