Skip to content

Instantly share code, notes, and snippets.

@tylermenezes
Created March 27, 2020 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylermenezes/6f28c6bc43c2904c23fb55093d0d4ac7 to your computer and use it in GitHub Desktop.
Save tylermenezes/6f28c6bc43c2904c23fb55093d0d4ac7 to your computer and use it in GitHub Desktop.
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