Skip to content

Instantly share code, notes, and snippets.

@stekhn
Last active April 23, 2020 13:12
Show Gist options
  • Save stekhn/46a1b1f07c47113d7c096860e8f5baa8 to your computer and use it in GitHub Desktop.
Save stekhn/46a1b1f07c47113d7c096860e8f5baa8 to your computer and use it in GitHub Desktop.
Simple implementation of the Hare-Niemeyer (or Hamilton or largest remainder) method for allocating parliament seats proportionally
const results = [
{ party: 'socialists', votes: 130755 },
{ party: 'conservatives', votes: 102068 },
{ party: 'liberals', votes: 34012 },
{ party: 'greens', votes: 31090 },
{ party: 'crazypeople', votes: 11111 }
];
const hareNiemeyer = (results, seats) => {
const totalVotes = results.reduce((total, party) => {
return total + party.votes;
}, 0);
const _results = results.map(party => {
return Object.assign({}, party, {
expectedSeats: seats * party.votes / totalVotes,
roundedSeats: Math.floor(seats * party.votes / totalVotes),
finalSeats: Math.floor(seats * party.votes / totalVotes)
});
}).sort((a, b) => b.expectedSeats % 1 - a.expectedSeats % 1);
const totalRoundedSeats = _results.reduce((total, party) => {
return total + party.roundedSeats;
}, 0);
const remainingSeats = seats - totalRoundedSeats;
for (let i = 0; i < remainingSeats; i++) {
_results[i].finalSeats += 1;
}
return _results;
};
console.log(hareNiemeyer(results, 420));
// [{
// party: 'conservatives',
// votes: 102068,
// expectedSeats: 138.71704267463986,
// roundedSeats: 138,
// finalSeats: 139
// },
// {
// party: 'socialists',
// votes: 130755,
// expectedSeats: 177.70453927697744,
// roundedSeats: 177,
// finalSeats: 178
// },
// {
// party: 'greens',
// votes: 31090,
// expectedSeats: 42.2533297091601,
// roundedSeats: 42,
// finalSeats: 42
// },
// {
// party: 'liberals',
// votes: 34012,
// expectedSeats: 46.22451753193803,
// roundedSeats: 46,
// finalSeats: 46
// },
// {
// party: 'crazypeople',
// votes: 11111,
// expectedSeats: 15.100570807284589,
// roundedSeats: 15,
// finalSeats: 15
// }]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment