Created
December 13, 2012 01:17
-
-
Save siddhantgoel/4273214 to your computer and use it in GitHub Desktop.
This file contains 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
#! /usr/bin/env python | |
import sys | |
import random | |
def generate(a, b): | |
n = len(a); n_round = 0; | |
while n > 1: | |
assert(len(a) == len(b)) # safety check | |
print "Round => " + str(n_round + 1) | |
if n % 2 == 0: | |
# construct teams | |
teams = list(); i = 0; | |
while i < n: | |
teams.append((a[i], b[i + 1])) | |
teams.append((a[i + 1], b[i])) | |
i = i + 2 | |
print "Teams => " + str(teams) | |
# play teams against one another and get the winners | |
winners = list(); i = 0; | |
while i < n: | |
winners.append(random.choice([teams[i], teams[i + 1]])) | |
i = i + 2 | |
print "Winners => " + str(winners) | |
# update the list of players | |
a, b = list(), list() | |
for w in winners: | |
a.append(w[0]) | |
b.append(w[1]) | |
else: | |
# hold one (random) pair out (so that they're not paired together either) | |
index = random.choice(xrange(0, n)) | |
a_index, b_index = a[index], b[index] | |
a = a[0:index] + a[index + 1:] | |
b = b[0:index] + b[index + 1:] | |
n = len(a) | |
# construct teams | |
teams = list(); i = 0; | |
while i < n: | |
teams.append((a[i], b[i + 1])) | |
teams.append((a[i + 1], b[i])) | |
i = i + 2 | |
print "Teams => " + str(teams) | |
print "(Held out " + str((a_index, b_index)) + ")" | |
# play teams against one another and get the winners | |
winners = list(); i = 0; | |
while i < n: | |
winners.append(random.choice([teams[i], teams[i + 1]])) | |
i = i + 2 | |
print "Winners => " + str(winners) | |
# update the list of players | |
a, b = list(), list() | |
for w in winners: | |
a.append(w[0]) | |
b.append(w[1]) | |
# add the team which was held out | |
a.append(a_index) | |
b.append(b_index) | |
print "\n" | |
n = len(a); n_round = n_round + 1 | |
print "Final winner => " + str((a[0], b[0])) + "\n" | |
if __name__ == "__main__": | |
try: | |
num_players = int(sys.argv[1]) | |
except: | |
print "Usage: python %s <num_players>" % sys.argv[0] | |
sys.exit(1) | |
else: | |
a = map(lambda x: 'a' + str(x), xrange(1, num_players + 1)) | |
b = map(lambda x: 'b' + str(x), xrange(1, num_players + 1)) | |
generate(a, b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment