我应该如何用 Javascript 解决这个组合场景?
How should I solve this combinations scenario with Javascript?
对于最少 8 支球队和最多 18 支球队的锦标赛,我必须确定比赛日历。锦标赛有 17 个回合或比赛日。所以每支球队在每个比赛日都必须遇到另一支球队。如果少于 18 支球队可以重复相遇,这样一支球队就可以多次与另一支球队交手。
This is an example for 18 teams tournament. And this would be a case for less than 18 teams fixture, here in particular 9 teams.
所以,我必须进行排列,然后将它们安排在不同的轮次上。我试过:
组合:
function k_combinations(set, k) {
var i, j, combs, head, tailcombs;
if (k > set.length || k <= 0) {
return [];
}
if (k == set.length) {
return [set];
}
if (k == 1) {
combs = [];
for (i = 0; i < set.length; i++) {
combs.push([set[i]]);
}
return combs;
}
combs = [];
for (i = 0; i < set.length - k + 1; i++) {
head = set.slice(i, i+1);
tailcombs = k_combinations(set.slice(i + 1), k - 1);
for (j = 0; j < tailcombs.length; j++) {
combs.push(head.concat(tailcombs[j]));
}
}
return combs;
}
var teams = [ {name: 'Real Madrid'},
{name: 'Las Palmas'},
{name: 'Alavés'},
{name: 'Valencia'},
{name: 'Sevilla'},
{name: 'Betis'},
{name: 'Córdoba'},
{name: 'Deportivo'},
{name: 'Atlético de Madrid'},
{name: 'Levante'},
{name: 'Rayo Vallecano'},
{name: 'Athletic Bilbao'},
{name: 'Osasuna'},
{name: 'Zaragoza'},
{name: 'Villareal'},
{name: 'Racing de Santander'},
{name: 'Espanyol'},
{name: 'Cádiz'},
];
// Compute whole encounters combinations.
var seasonMatches = k_combinations(teams,2);
回合组合排列:
var calendar = {};
for (var i = 0; i<17; i++) {
calendar[i+1] = [];
}
var encounters = seasonMatches;
for (var i = 0; i<Object.keys(calendar).length; i++) {
encounters.map(function (match,index) {
if (! _.any(calendar, function (m) {
return m[0].name === match[0].name || m[1].name === match[1].name || m[0].name === match[1].name || m[1].name === match[0].name;
})) {
calendar[i+1].push(match);
}
});
}
我正在使用 lodash 来简化对上一轮遭遇战的存在性检查。
我遇到的问题是,通过这种方式我在日历中的每一轮都会遇到相同的情况。而且,如果我将拼接添加到 seasonMatches,我最终每轮都会有不同的匹配项。
I've got a fiddle with this example shown above.
我应该如何解决这个问题?
看来你喜欢努力工作:)还有一个更简单的方法(jsbin link):
var teamsCount = 9;
var matchDays = 17;
var matches = [];
var teams = _.shuffle(_.range(teamsCount));
while(matches.length < matchDays){
var newMatches = _(teams).chunk(2).partition(function(match){
return match.length === 2;
}).value();
matches = matches.concat(newMatches[0]);
if(newMatches[1].length) { // one team was left out, let's make sure it is playing
// we put it first, and add the other teams, shuffled, without that one team
teams = newMatches[1][0].concat(_.shuffle(_.without(_.range(teamsCount), newMatches[1][0][0])));
} else {
teams = _.shuffle(_.range(teamsCount));
}
}
// we might get more then we need
matches = _.take(matches, matchDays);
_.each(matches, function(match, index){
console.log('round ' + index + ': ' + match);
});
说明:
由于您没有施加其他限制(例如,每支球队必须互相比赛),因此只需将球队带走,将他们洗牌并一次将他们分成 2 个(= 一场比赛)。然后我们将块划分为真正的比赛(2 个团队数组)和剩下的(1 个团队数组)。
我们采用真实匹配项并将它们添加到现有匹配项中。如果我们有剩余的,我们保留它,并将洗牌后的团队(没有剩余的团队)连接到它并再次分块。我们继续,直到我们有足够的比赛。由于我们可能会得到比我们需要的更多的匹配项,所以我们只取前 17 个。
JSbin 更精细,也可以将匹配项转换为团队名称。
我试图查看您的代码,看看为什么您会得到您显示的模式,但它太复杂了,我无法理解,我喜欢以简单的方式做事;-)
对于最少 8 支球队和最多 18 支球队的锦标赛,我必须确定比赛日历。锦标赛有 17 个回合或比赛日。所以每支球队在每个比赛日都必须遇到另一支球队。如果少于 18 支球队可以重复相遇,这样一支球队就可以多次与另一支球队交手。
This is an example for 18 teams tournament. And this would be a case for less than 18 teams fixture, here in particular 9 teams.
所以,我必须进行排列,然后将它们安排在不同的轮次上。我试过:
组合:
function k_combinations(set, k) {
var i, j, combs, head, tailcombs;
if (k > set.length || k <= 0) {
return [];
}
if (k == set.length) {
return [set];
}
if (k == 1) {
combs = [];
for (i = 0; i < set.length; i++) {
combs.push([set[i]]);
}
return combs;
}
combs = [];
for (i = 0; i < set.length - k + 1; i++) {
head = set.slice(i, i+1);
tailcombs = k_combinations(set.slice(i + 1), k - 1);
for (j = 0; j < tailcombs.length; j++) {
combs.push(head.concat(tailcombs[j]));
}
}
return combs;
}
var teams = [ {name: 'Real Madrid'},
{name: 'Las Palmas'},
{name: 'Alavés'},
{name: 'Valencia'},
{name: 'Sevilla'},
{name: 'Betis'},
{name: 'Córdoba'},
{name: 'Deportivo'},
{name: 'Atlético de Madrid'},
{name: 'Levante'},
{name: 'Rayo Vallecano'},
{name: 'Athletic Bilbao'},
{name: 'Osasuna'},
{name: 'Zaragoza'},
{name: 'Villareal'},
{name: 'Racing de Santander'},
{name: 'Espanyol'},
{name: 'Cádiz'},
];
// Compute whole encounters combinations.
var seasonMatches = k_combinations(teams,2);
回合组合排列:
var calendar = {};
for (var i = 0; i<17; i++) {
calendar[i+1] = [];
}
var encounters = seasonMatches;
for (var i = 0; i<Object.keys(calendar).length; i++) {
encounters.map(function (match,index) {
if (! _.any(calendar, function (m) {
return m[0].name === match[0].name || m[1].name === match[1].name || m[0].name === match[1].name || m[1].name === match[0].name;
})) {
calendar[i+1].push(match);
}
});
}
我正在使用 lodash 来简化对上一轮遭遇战的存在性检查。
我遇到的问题是,通过这种方式我在日历中的每一轮都会遇到相同的情况。而且,如果我将拼接添加到 seasonMatches,我最终每轮都会有不同的匹配项。
I've got a fiddle with this example shown above. 我应该如何解决这个问题?
看来你喜欢努力工作:)还有一个更简单的方法(jsbin link):
var teamsCount = 9;
var matchDays = 17;
var matches = [];
var teams = _.shuffle(_.range(teamsCount));
while(matches.length < matchDays){
var newMatches = _(teams).chunk(2).partition(function(match){
return match.length === 2;
}).value();
matches = matches.concat(newMatches[0]);
if(newMatches[1].length) { // one team was left out, let's make sure it is playing
// we put it first, and add the other teams, shuffled, without that one team
teams = newMatches[1][0].concat(_.shuffle(_.without(_.range(teamsCount), newMatches[1][0][0])));
} else {
teams = _.shuffle(_.range(teamsCount));
}
}
// we might get more then we need
matches = _.take(matches, matchDays);
_.each(matches, function(match, index){
console.log('round ' + index + ': ' + match);
});
说明: 由于您没有施加其他限制(例如,每支球队必须互相比赛),因此只需将球队带走,将他们洗牌并一次将他们分成 2 个(= 一场比赛)。然后我们将块划分为真正的比赛(2 个团队数组)和剩下的(1 个团队数组)。
我们采用真实匹配项并将它们添加到现有匹配项中。如果我们有剩余的,我们保留它,并将洗牌后的团队(没有剩余的团队)连接到它并再次分块。我们继续,直到我们有足够的比赛。由于我们可能会得到比我们需要的更多的匹配项,所以我们只取前 17 个。
JSbin 更精细,也可以将匹配项转换为团队名称。
我试图查看您的代码,看看为什么您会得到您显示的模式,但它太复杂了,我无法理解,我喜欢以简单的方式做事;-)