beyin fırtınası lazım acil |
4 Mesajlar | 811 Okunma |
Olası İhtimal:
[
{
teaml: 'Galatasaray',
team2: 'Fenerbahçe',
team1Score: 4,
team2Score: 0
},
{
teaml: 'Galatasaray',
team2: 'Trabzonspor',
team1Score: 0,
team2Score: 1
},
{
teaml: 'Fenerbahçe',
team2: 'Beşiktaş',
team1Score: 2,
team2Score: 1
},
{
teaml: 'Beşiktaş',
team2: 'Trabzonspor',
team1Score: 0,
team2Score: 2
}
]
...
...
...
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 teamsWithDetails = [
{"team": "Galatasaray", "concededGoal": 2, "scoredGoal": 4},
{"team": "Fenerbahçe", "concededGoal": 3, "scoredGoal": 2},
{"team": "Beşiktaş", "concededGoal": 4, "scoredGoal": 1},
{"team": "Trabzonspor", "concededGoal": 1, "scoredGoal": 3}
];
var matchupLimit = 2;
var matchPossibilites = [];
for (let team of teamsWithDetails) {
for (let opponent of teamsWithDetails) {
if (team.team == opponent.team) {
continue;
}
for (var i = team.scoredGoal; i >= 0; i--) {
for (var j = opponent.scoredGoal; j >= 0; j--) {
var possibility = {
"teaml": team.team,
"team2": opponent.team,
"team1Score": i,
"team2Score": j,
}
if (!matchPossibilites.some(
match => match.teaml === opponent.team
&& match.team2 === team.team
&& match.team1Score === j
&& match.team2Score === i
)) {
matchPossibilites.push(possibility);
}
}
}
};
};
var possibleSetsOfMatches = k_combinations(matchPossibilites, matchupLimit * 2);
for (let index = 0; index < possibleSetsOfMatches.length; index++) {
const match = possibleSetsOfMatches[index];
var checkTeamsMatchCount = new Array(teamsWithDetails.length).fill(false);
var checkTeamsScoreCount = new Array(teamsWithDetails.length).fill(false);
var checkTeamsConcededCount = new Array(teamsWithDetails.length).fill(false);
for (let i = 0; i < teamsWithDetails.length; i++) {
const team = teamsWithDetails[i];
var counter = 0;
var GoalCounter = 0;
var ConcededCounter = 0;
for (let j = 0; j < match.length; j++) {
const groupMember = match[j];
if (team.team == groupMember.teaml || team.team == groupMember.team2) {
counter++;
}
if (team.team == groupMember.teaml) {
GoalCounter += groupMember.team1Score;
}
if (team.team == groupMember.team2) {
GoalCounter += groupMember.team2Score;
}
}
if (counter == matchupLimit) {
checkTeamsMatchCount[i] = true;
} else {
continue;
}
if (GoalCounter == team.scoredGoal) {
checkTeamsScoreCount[i] = true;
} else {
continue;
}
}
let checker = arr => arr.every(v => v === true);
if (checker(checkTeamsMatchCount) && checker(checkTeamsScoreCount)) {
var flag = false;
for (let index = 0; index < match.length; index++) {
const element = match[index];
for (let i = index + 1; i < match.length; i++) {
const element1 = match[i];
if ((element.team1 == element1.team2) && (element.team2 == element1.team1)){
flag = true;
}
}
}
if (flag == false){
console.log("Olası İhtimal: ")
console.log(match);
}
}
}