Created
April 20, 2024 02:34
-
-
Save JadenGeller/786203450ad49688f962866f3c4d2866 to your computer and use it in GitHub Desktop.
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
func apportion(quota: Int, _ population: [Int]) -> [Int] { | |
var portions = population.map({ $0 / quota }) | |
let errors = population.map({ $0 % quota }) | |
let adjustments = (errors.reduce(0, +) + quota / 2) / quota | |
errors | |
.enumerated() | |
.sorted(by: { $0.element > $1.element }) | |
.prefix(adjustments) | |
.forEach { | |
portions[$0.offset] += 1 | |
} | |
return portions | |
} |
Author
JadenGeller
commented
Apr 20, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment