第15组员工分为3组

时间:2017-08-30 18:45:00

标签: javascript node.js express

我有9个用户,我想组成团队进行培训。每个团队最多应有3个用户,每个用户应在培训结束时与每个用户配对。

我正在使用expressjs

var express = require('express');
var _ = require('underscore');
var app = express();

app.get('/', function (req, res) {

// Names
var users = [
    {name: 'Parul Panchal'},
    {name: 'JJ Rademan'},
    {name: 'Mohamed Bassa'},
    {name: 'Naomi'},
    {name: 'Puella Lunsiswa'},
    {name: 'Pumla Kaleni'},
    {name: 'Jamie Gibbons'},
    {name: 'Charles'},
    {name: 'Mocheku Maseko'}
];

var groups = [];
var numberOfEmployees = 9, group = 3;
var numberOfGroups = numberOfEmployees*group;

for (var i = 0; i < numberOfGroups; i++) {
    groups.push({group: []});
}

groups.forEach(function (group) {
    group.group.forEach(function(g){
        users.forEach(function (user) {
            if(group.group.length < 3 ){
                g.push({name: user.name});
            }
        });
    });
});

res.send(groups);
});

app.listen(3000);

响应:      [{"group":[]},{"group":[]},{"group":[]},{"group":[]},{"group":[]}, {"group":[]},{"group":[]},{"group":[]},{"group":[]},{"group":[]}, {"group":[]},{"group":[]},{"group":[]},{"group":[]},{"group":[]}, {"group":[]},{"group":[]},{"group":[]},{"group":[]},{"group":[]}, {"group":[]},{"group":[]},{"group":[]},{"group":[]},{"group":[]}, {"group":[]},{"group":[]}]

1 个答案:

答案 0 :(得分:0)

这不是最佳解决方案,但它应该有效。

function initpairrecord(){
    uu=users.map((a,i)=>(users.map((b,j)=>(i==j?1:0))));
}
function pick_group_members(ui,picked,num){
    if(num <= 0)
        return picked;
    var paired = uu[ui];
    var topick = paired.findIndex((p,i)=>(p<=0&&!picked.includes(i)));
    if(topick < 0 && num > 0){
        var cand = users.map((u,i)=>i)
                        .filter((u,i)=>(i!=ui&&!picked.includes(i)));
        if(cand.length <= 0)
            throw new Error('too few users');
        topick = cand.reduce((a,c)=>(paired[c] < paired[a] ? c : a));
    }
    return pick_group_members(ui,picked.concat(topick),num-1);
}
function group_for_one_user(ui){
    var paired = uu[ui];
    var newgrouplist = [];
    while(!paired.every((p)=>(p>0))){
        var newgroup = pick_group_members(ui,[ui],groupsize-1);
        for(var i=0;i<newgroup.length; i++){
            for(var j=i+1;j<newgroup.length; j++){
                uu[newgroup[i]][newgroup[j]]++;
                uu[newgroup[j]][newgroup[i]]++;
            }
        }
        newgrouplist.push(newgroup);
    }
    return newgrouplist;
}
function group_user_index(){
    var groups = [];
    for(var i=0; i < users.length; i ++){
        groups = groups.concat(group_for_one_user(i));
    }
    return groups;
}
function findsolution(){
}
function groupproblem(i_users,i_groupsize){
    users = i_users;
    groupsize = i_groupsize;
    initpairrecord();
    var groups = group_user_index();
    return groups.map((g)=>(
        {'group': g.map((i)=>({'name': users[i].name}))}
    ));
}

given = [
    {name: 'Parul Panchal'},
    {name: 'JJ Rademan'},
    {name: 'Mohamed Bassa'},
    {name: 'Naomi'},
    {name: 'Puella Lunsiswa'},
    {name: 'Pumla Kaleni'},
    {name: 'Jamie Gibbons'},
    {name: 'Charles'},
    {name: 'Mocheku Maseko'}
];
sol = groupproblem(given,3);
console.log(sol);