Skip to content

Instantly share code, notes, and snippets.

@scionoftech
Created August 23, 2019 08:07
Show Gist options
  • Save scionoftech/01ac2bdb22a9c34d9d95c863418bd7e7 to your computer and use it in GitHub Desktop.
Save scionoftech/01ac2bdb22a9c34d9d95c863418bd7e7 to your computer and use it in GitHub Desktop.
This a python script for generating roundrobin matches for sporting events
from itertools import combinations
from operator import itemgetter
def getroundrobinmatches(participants):
rounds = participants-1 if participants%2 == 0 else participants
data = list(range(1,participants+1))
comb = combinations(data, 2)
matches = []
rr = 1
for x in comb:
for y in data:
if y == x[0]:
matches.append({"participant1":x[0],"participant2":x[1],"round":rr})
rr += 1
if rr > rounds:
rr = 1
mat = 1
for rou in range(1,rounds+1):
for i in range(len(matches)):
if matches[i]["round"] == rou:
matches[i]["match"] = mat
mat += 1
matches = sorted(matches, key=itemgetter('match'))
return matches
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment