Created
March 27, 2020 18:24
-
-
Save tylermenezes/6f28c6bc43c2904c23fb55093d0d4ac7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def chunk(list, n): | |
"""Breaks a list into chunks of size n.""" | |
return [ list[i:i+n] for i in range(0, len(list), n)] | |
def balanceGroups(groups): | |
"""Balances a list of lists, so they are roughly equally sized.""" | |
numPlayers = sum([len(group) for group in groups]) | |
minGroupSize = int(numPlayers/len(groups)) | |
groupsArr = list(enumerate(groups)) | |
for i, group in groupsArr: | |
while(len(group) < minGroupSize): | |
for j, groupSteal in groupsArr: | |
if (i != j) and len(groupSteal) > minGroupSize: | |
group.append(groupSteal.pop()) | |
return [group for group in groups] | |
def makeGroups(players, groupSize): | |
"""Makes even-ish groups of size groupSize and return an array of the players.""" | |
if len(players) == 1: | |
return [] | |
groups = chunk(players, groupSize) | |
if len(groups) == 1: | |
return groups | |
return balanceGroups(groups) | |
print(makeGroups([1,2,3,4,5,6,7,8,9], 4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment