Skip to content

Instantly share code, notes, and snippets.

@renatoinline
Last active February 24, 2018 14:51
Show Gist options
  • Select an option

  • Save renatoinline/07b86cfc3bcf83dea780897d9b5271a4 to your computer and use it in GitHub Desktop.

Select an option

Save renatoinline/07b86cfc3bcf83dea780897d9b5271a4 to your computer and use it in GitHub Desktop.
var lines = [
{id: 'L1'}, {id: 'L2'}
];
var demands = [
{id: 'D1'}, {id: 'D2'}, {id: 'D3'}
];
var line_demands = [];
var combinations = [];
var tempCombs = [];
for (let indexD = 0; indexD < demands.length; indexD++) {
const elementD = demands[indexD];
for (let indexL = 0; indexL < lines.length; indexL++) {
const elementL = lines[indexL];
line_demands.push({id: elementL.id + '_' + elementD.id});
}
}
for (let index = 0; index < line_demands.length; index++) {
console.log(line_demands[index]);
}
console.log("\n");
comb(0, 3);
function comb(offset, k){
if(k == 0){
// todo: remove invalid inputs in advanced
combinations.push(tempCombs.slice()); // slice to avoid reference
return;
}
else{
for (let index = offset; index <= line_demands.length - k; index++) {
tempCombs.push(line_demands[index]);
comb(index + 1, k - 1);
tempCombs.pop();
}
}
}
// todo: remove global bad inputs???
console.log(combinations);
function getDemand(element){
return element.substring(3);
}
/*
{ id: 'L1_D1' }
{ id: 'L2_D1' }
{ id: 'L1_D2' }
{ id: 'L2_D2' }
var result = [
[ { id: 'L1_D1' }, { id: 'L2_D1' } ], // invalido
[ { id: 'L1_D2' }, { id: 'L2_D2' } ], // invalido
[ { id: 'L1_D1' }, { id: 'L1_D2' } ], // pessimo
[ { id: 'L2_D1' }, { id: 'L2_D2' } ], // pessimo
[ { id: 'L2_D1' }, { id: 'L1_D2' } ], // otimo
[ { id: 'L1_D1' }, { id: 'L2_D2' } ], // otimo
]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment