Skip to content

Instantly share code, notes, and snippets.

@aronasorman
Last active December 20, 2015 05:49
Show Gist options
  • Save aronasorman/6081338 to your computer and use it in GitHub Desktop.
Save aronasorman/6081338 to your computer and use it in GitHub Desktop.
Random pairings
from itertools import cycle
import random
def pairup(core, interns=None):
'''
Assigns a name to another name. Assumes that the names are unique.
'''
# assign the core group
random.shuffle(core)
lucky = None
if len(core) % 2 != 0:
# pick lucky core person to be alone
lucky = core.pop()
pairings = [[core[i], core[i+1]] for i in range(0, len(core)-1, 2)]
if lucky:
pairings += [[lucky]]
# assign them interns
if interns:
pairings.sort(key=len) # to prioritize the lucky person
random.shuffle(interns)
# round robin style of assigning until we run out of interns
pairs = cycle(pairings)
for intern in interns:
pair = next(pairs)
pair.append(intern)
return map(tuple, pairings)
def main():
core = ['ben', 'dylan', 'jamie', 'guan', 'richard', 'rui', 'alexsandra', 'steve']
interns = ['pranav', 'angelique', 'kevin', 'andres', 'aron']
groups = pairup(core, interns)
for i, group in enumerate(groups):
print "Group %d" % (i + 1)
for person in group:
print " %s" % person
if __name__ == '__main__':
main()
@aronasorman
Copy link
Author

the generation of the pairings dict could be simplified to dict(pair for pair in zip(half1, half2)), but it's the same as this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment