Skip to content

Instantly share code, notes, and snippets.

@tekton
Created August 16, 2017 23:26
Show Gist options
  • Save tekton/d06aa8f1489427b97add029b5020ec16 to your computer and use it in GitHub Desktop.
Save tekton/d06aa8f1489427b97add029b5020ec16 to your computer and use it in GitHub Desktop.
Randomized brackets of randomization
import random
import pprint
import copy
from collections import defaultdict
root_people = [
"P1",
"P2",
"P3",
"P4",
"P5",
"P6",
"P7",
"P8",
"P9",
"P0"
]
person_matrix = {}
round_matrix = defaultdict(list)
while_marker = {}
def create_person_matrix():
for person in root_people:
person_matrix[person] = {
"Round 1": None,
"Round 2": None,
"Round 3": None,
"Round 4": None,
"Round 5": None,
"Round 6": None,
}
def get_two_people(_people, x=None, y=None):
if not x:
x = random.randint(0, len(_people)-1)
if not y:
y = random.randint(0, len(_people)-1)
while x == y:
y = random.randint(0, len(_people)-1)
return x, y
def check_previous_matches(_people, x, y, i):
for ii in range(1, i):
print("Checking matchups for {} in Round {} against {}".format(_people[x], ii, _people[y]))
if person_matrix[_people[x]]["Round {}".format(ii)] == _people[y]:
print("Got a match in previous round: {}".format(ii))
return True
return False
def find_opponent(person, remaining_people, round=None):
# take the list of people this person has already fought...
previous = []
for x in person_matrix[person]:
if x is not None:
previous.append(person_matrix[person][x])
_people = copy.deepcopy(remaining_people)
people = list(set(_people) - set(previous))
print(person, previous, people, _people)
random.shuffle(people)
print("Shuffled people: {}", format(people))
return people[0]
def gen_week(matches=5):
for i in xrange(1, matches, 1):
print("***** {} *****".format(i))
_people = copy.deepcopy(root_people)
while_marker[i] = True
while while_marker[i]:
random.shuffle(_people)
x = _people[0]
y = find_opponent(x, _people[1:])
person_matrix[x]["Round {}".format(i)] = y
person_matrix[y]["Round {}".format(i)] = x
round_matrix["Round {}".format(i)].append((x, y))
del(_people[0])
for j, val in enumerate(_people):
if val == y:
y = j
break
del(_people[y])
if len(_people) == 0:
while_marker[i] = False
pprint.pprint(person_matrix)
for i in xrange(1, 7, 1):
print("Round {}".format(i), round_matrix["Round {}".format(i)])
create_person_matrix()
gen_week(matches=7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment