I have a coin collection and I want to distribute that collection evenly (or as evenly as possible) over my albums. The coins are in 353 sheets and there are 28 albums, so that's 12.81 sheets per album. There are 193 countries and not every country has the same amount of sheets. Afghanistan has 1 sheet, as does Albania. Algeria has two sheets, Andorra and Angola both have 1 sheet, Argentina has 6 sheets and so on.
Basically it comes down to the following: I have a collection of 193 numbers (the number of sheets per country) and those numbers need to be split in 28 collections. The sum of each collection should be as close to each other as possible. There are two important limitations. The maximum sum of numbers for a collection is 15 and the order of the numbers can not be changed. This is the collection:
{1,1,2,1,1,6,1,1,6,4,1,2,2,1,1,1,8,1,1,1,1,1,1,5,1,1,1,3,1,1,1,1,1,1,2,2,2,1,2,1,2,2,2,3,1,2,5,1,2,1,2,2,4,1,1,1,1,2,2,1,1,2,3,1,1,1,2,2,5,2,2,1,1,1,1,2,4,2,2,2,1,3,2,3,5,2,3,2,2,3,2,1,1,1,1,1,1,1,1,1,2,3,1,1,1,2,1,3,1,2,3,1,2,1,3,1,1,1,2,1,2,1,1,2,1,3,1,1,2,2,1,2,3,3,5,4,1,1,1,3,1,1,1,2,1,2,2,1,1,2,1,1,1,1,1,6,1,1,6,3,1,3,2,1,1,2,1,3,1,1,1,1,4,1,1,2,4,1,1,2,1,3,1,1,3,3,1,1,1,1,5,1,2}
If I would do it by hand, it would result in something like this:
1. {1,1,2,1,1,6,1,1} = 14
2. {6,4,1,2} = 13
3. {2,1,1,1,8} = 13
4. {1,1,1,1,1,1,5,1,1} = 13
5. {1,3,1,1,1,1,1,1,2} = 12
6. {2,2,1,2,1,2,2} = 12
7. {2,3,1,2,5} = 13
8. {1,2,1,2,2,4} = 12
9. {1,1,1,1,2,2,1,1,2} = 12
10. {3,1,1,1,2,2,5} = 15
11. {2,2,1,1,1,1,2} = 10
12. {4,2,2,2,1,3} 14
13. {2,3,5,2} = 12
14. {3,2,2,3,2} = 12
15. {1,1,1,1,1,1,1,1,1,2} = 11
16. {3,1,1,1,2,1,3,1} = 13
17. {2,3,1,2,1,3} = 12
18. {1,1,1,2,1,2,1,1,2} = 12
19. {1,3,1,1,2,2,1,2} = 13
20. {3,3,5} = 11
21. {4,1,1,1,3,1,1,1} = 13
22. {2,1,2,2,1,1,2,1,1} = 13
23. {1,1,1,6,1,1} = 11
24. {6,3,1,3} = 13
25. {2,1,1,2,1,3,1,1,1} = 13
26. {1,4,1,1,2,4} = 13
27. {1,1,2,1,3,1,1,3} = 13
28. {3,1,1,1,1,5,1,2} = 15
I really don't know how I should do this in code. Could someone point me in the right direction? Thanks a lot!
I agree with @Peter Csala. This can be resolved with PowerShell by example. Up to you to know how to launch this script to see the result.
$x = @(1,1,2,1,1,6,1,1,6,4,1,2,2,1,1,1,8,1,1,1,1,1,1,5,1,1,1,3,1,1,1,1,1,1,2,2,2,1,2,1,2,2,2,3,1,2,5,1,2,1,2,2,4,1,1,1,1,2,2,1,1,2,3,1,1,1,2,2,5,2,2,1,1,1,1,2,4,2,2,2,1,3,2,3,5,2,3,2,2,3,2,1,1,1,1,1,1,1,1,1,2,3,1,1,1,2,1,3,1,2,3,1,2,1,3,1,1,1,2,1,2,1,1,2,1,3,1,1,2,2,1,2,3,3,5,4,1,1,1,3,1,1,1,2,1,2,2,1,1,2,1,1,1,1,1,6,1,1,6,3,1,3,2,1,1,2,1,3,1,1,1,1,4,1,1,2,4,1,1,2,1,3,1,1,3,3,1,1,1,1,5,1,2)
$sum = 0
$cols = [System.Collections.Generic.List[System.Collections.Generic.List[System.Int32]]]::new()
$col = [System.Collections.Generic.List[System.Int32]]::new();
for($i=0; $i -lt $x.Count - 1; $i++) {
$sum += $x[$i]
if ($sum + $x[$i+1] -gt 15) {
$cols.Add($col)
$col = [System.Collections.Generic.List[System.Int32]]::new();
$sum = 0;
} else {
$col.Add($x[$i])
}
}
for($i=0; $i -lt $cols.Count; $i++) {
Write-Host "Collection $i : $($cols[$i] -join ',')"
}