-
Notifications
You must be signed in to change notification settings - Fork 437
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use method of largest remainder to count poll votes
Signed-off-by: Maksim Sukharev <[email protected]>
- Loading branch information
Showing
3 changed files
with
119 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import { calculateVotePercentage } from '../calculateVotePercentage.ts' | ||
|
||
describe('calculateVotePercentage', () => { | ||
const tests = [ | ||
[0, [], 0], | ||
[1, [1], 100], | ||
// Math rounded to 100% | ||
[4, [1, 3], 100], | ||
[11, [1, 2, 8], 100], | ||
[13, [11, 2], 100], | ||
[13, [9, 4], 100], | ||
[26, [16, 5, 5], 100], | ||
// Rounded to 100% by largest remainder | ||
[1000, [132, 494, 92, 282], 100], | ||
[1000, [135, 480, 97, 288], 100], | ||
// Best effort is 99% | ||
[3, [1, 1, 1], 99], | ||
[7, [2, 2, 3], 99], | ||
[1000, [133, 491, 93, 283], 99], | ||
[1000, [134, 488, 94, 284], 99], | ||
// Best effort is 98% | ||
[1000, [136, 482, 96, 286], 98], | ||
[1000, [135, 140, 345, 95, 285], 98], | ||
// Best effort is 97% | ||
[1000, [137, 132, 347, 97, 287], 97], | ||
] | ||
|
||
it.each(tests)('test %d votes in %o distribution rounds to %d%%', (total, votes, result) => { | ||
const percentageMap = calculateVotePercentage(votes, total) | ||
|
||
expect(votes.reduce((a, b) => a + b, 0)).toBe(total) | ||
expect(percentageMap.reduce((a, b) => a + b, 0)).toBe(result) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
/** | ||
* Finds indexes of largest remainders to distribute quota | ||
* @param array array of numbers to compare | ||
*/ | ||
function getLargestIndexes(array: number[]) { | ||
let maxValue = 0 | ||
const maxIndexes: number[] = [] | ||
|
||
for (let i = 0; i < array.length; i++) { | ||
if (array[i] > maxValue) { | ||
maxValue = array[i] | ||
maxIndexes.length = 0 | ||
maxIndexes.push(i) | ||
} else if (array[i] === maxValue) { | ||
maxIndexes.push(i) | ||
} | ||
} | ||
|
||
return maxIndexes | ||
} | ||
|
||
/** | ||
* Provide percentage distribution closest to 100 by method of largest remainder | ||
* @param votes array of given votes | ||
* @param total amount of votes | ||
*/ | ||
export function calculateVotePercentage(votes: number[], total: number) { | ||
if (!total) { | ||
return votes | ||
} | ||
|
||
const rounded: number[] = [] | ||
const wholes: number[] = [] | ||
const remainders: number[] = [] | ||
let sumRounded = 0 | ||
let sumWholes = 0 | ||
|
||
for (const i in votes) { | ||
const quota = votes[i] / total * 100 | ||
rounded.push(Math.round(quota)) | ||
wholes.push(Math.floor(quota)) | ||
remainders.push(Math.round((quota % 1) * 1000)) | ||
sumRounded += rounded[i] | ||
sumWholes += wholes[i] | ||
} | ||
|
||
// Check if simple round gives 100% | ||
if (sumRounded === 100) { | ||
return rounded | ||
} | ||
|
||
// Increase values by largest remainder method if difference allows | ||
for (let i = 100 - sumWholes; i > 0;) { | ||
const largest = getLargestIndexes(remainders) | ||
if (largest.length > i) { | ||
return wholes | ||
} | ||
|
||
for (const idx of largest) { | ||
wholes[idx]++ | ||
remainders[idx] = 0 | ||
i-- | ||
} | ||
} | ||
|
||
return wholes | ||
} |