で連立の組み合わせを作成します。各配列の最初の値が、政党の大文字の名前です、私は次のような2次元配列を持つJavascript配列
let electionResultsData = [
["VVD", "vvd", 20.5, 2504948],
["PVDA", "pvda", 19.6, 2340750],
["PVV", "pvv", 15.4, 950263],
["CDA", "cda", 13.6, 801620],
["SP", "sp", 9.8, 909853],
["D66", "d66", 6.9, 757091],
["GL", "gl", 6.7, 219896],
["CU", "cu", 3.2, 294586],
["SGP", "sgp", 1.7, 196780],
["PVDD", "pvdd", 1.3, 182162],
["50PLUS", "50plus", 0.9, 177631],
["OVERIG", "overig", 0.2, 51463],
["PIRATEN", "piraten", 0.1, 30600],
["LP", "lp", 0.1, 3335],
["PVDM", "pvdm", 0.1, 3257],
["JEZUSLFT", "jezuslft", 0, 0],
["ONDRNMR", "ondrnmr", 0, 0],
["LOKAAL", "lokaal", 0, 0],
["ARTIKEL1", "artikel1", 0, 0],
["GEENPEIL", "geenpeil", 0, 0],
["VRIJP", "vrijp", 0, 0],
["BURGBEW", "burgbew", 0, 0],
["FVD", "fvd", 0, 0],
["VDP", "vdp", 0, 0],
["NIEUWEW", "nieuwew", 0, 0],
["DENK", "denk", 0, 0],
["STEMNL", "stemnl", 0, 0],
["VNL", "vnl", 0, 0]
]
、2番目の値は小文字です3番目の値は投票の割合で、4番目の値は投票の数です。参考までに、オランダの政党です。
私が今しなければならないことは、可能な連合を計算するシステムを作ることです。当事者間の連立は、当事者が議会で75人以上(少なくとも76人)の議席を獲得する場合にのみ達成することができる。私はこのコードで上記の配列をループにループを作りました:
/**
* Form coalitions of not more than six parties that exceed 75 parliament seats
*/
formCoalitions(electionResultsData) {
let coalitions = [];
let maxNumberOfCoalitionParties = 6;
// Main loop to check all possible coalitions (28 parties * 28 = 784)
for (let i = 0; i < 784; i ++) {
let coalitionSeats = 0;
let partySeats = 0;
let coalitionParties = [];
let coalition = [];
// The inner loop to generate a combination/coalition
for (let i = 0; i < electionResultsData.length; i++) {
// Check if a coalition has formed yet
if (coalitionSeats < 76) {
partySeats = (150/100) * electionResultsData[i][2];
coalitionSeats += partySeats;
coalitionParties.push(electionResultsData[i][0]);
// If coalition has formed
if (coalitionSeats > 75) {
// Push data into a second dimension coalition array
coalition[0] = parseInt(coalitionSeats);
coalition[1] = coalitionParties;
// Check if the generated coalition array already exists
let coalitionsStringified = JSON.stringify(coalitions);
let coalitionStringified = JSON.stringify(coalition);
let coalitionExists = coalitionsStringified.indexOf(coalitionStringified);
if (coalitionExists === -1) {
coalitions.push(coalition);
}
}
}
}
}
// Loop through the coalitions (the charts will be drawn here)
for (let i = 0; i < coalitions.length; i++) {
console.log(coalitions[i]);
}
}
問題があるが、このコードは、唯一の可能な連合ではなく、すべての可能な連合を生成します。私は何とか生成された組み合わせを保存し、同じ連合を生成せずにループを再実行する必要があります。このループは、6つ以下の当事者との可能なすべての連合が生成されるまで、これを続けなければならない。その後、ループは停止することができます。
これにはどのような方法が最適ですか?
当事者にシートを配布する[複雑なシステム(https://en.wikipedia.org/wiki/Elections_in_the_Netherlands#Seat_assignment)のビットがあります。これを組み入れたいですか?あなたは分数座席で今働くようです。 –
これを組み込む必要はありません。私はそれを説明する方法は私がそれを作る方法を伝えた方法です:( –
あなたの質問を再投稿しないでください、私たちのコミュニティはこれを嫌に思うことがよくあります。あなたの質問に注意を喚起したい場合は、[未回答の質問に対する注意を喚起する](https://meta.stackexchange.com/q/7046) –