Skip to content

Instantly share code, notes, and snippets.

Created January 20, 2014 22:03
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 anonymous/11b21971e475a2e189c3 to your computer and use it in GitHub Desktop.
Save anonymous/11b21971e475a2e189c3 to your computer and use it in GitHub Desktop.
#-------------------------------------------------
# imports
#-------------------------------------------------
import sys
import random
from pprint import pprint
#-----------------------------------------------
# read file in, validate even number of players
#-------------------------------------------------
filename = sys.argv[1]
names = [name.strip() for name in open(filename)]
if len(names)%2 != 0:
sys.exit("need even number of players")
#-------------------------------------------------
#
#-------------------------------------------------
family_dict = {}
for name in names:
first_name, last_name = name.split()
if last_name not in family_dict:
family_dict[last_name] = []
family_dict[last_name].append(first_name)
pairs = []
while len(family_dict) > 0:
#get the largest family so we don't end up with two members of the same family left at the end
largest_family=sorted(family_dict, key=lambda k: len(family_dict[k]), reverse=True).pop(0)
#get a random family that is NOT the largest family
sample = family_dict.keys()
sample.remove(largest_family)
random_family=random.choice(sample)
#get two players from the two families
player1 = random.choice(family_dict[largest_family])
player2 = random.choice(family_dict[random_family])
pairs.append([player1+" "+largest_family, player2+" "+random_family])
#remove the two players from the dictionary
family_dict[largest_family].remove(player1)
family_dict[random_family].remove(player2)
if len(family_dict[largest_family]) == 0:
del family_dict[largest_family]
if len(family_dict[random_family]) == 0:
del family_dict[random_family]
print pairs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment