Skip to content

Instantly share code, notes, and snippets.

@mikeyakymenko
Forked from ih84ds/round-robin.py
Created May 29, 2018 21:30
Show Gist options
  • Save mikeyakymenko/bd066307c26f41104c43664cb8c37297 to your computer and use it in GitHub Desktop.
Save mikeyakymenko/bd066307c26f41104c43664cb8c37297 to your computer and use it in GitHub Desktop.
Generate Round Robin Schedule with Balanced Home/Away in Python
def create_balanced_round_robin(players):
""" Create a schedule for the players in the list and return it"""
s = []
if len(players) % 2 == 1: players = players + [None]
# manipulate map (array of indexes for list) instead of list itself
# this takes advantage of even/odd indexes to determine home vs. away
n = len(players)
map = list(range(n))
mid = n // 2
for i in range(n-1):
l1 = map[:mid]
l2 = map[mid:]
l2.reverse()
round = []
for j in range(mid):
t1 = players[l1[j]]
t2 = players[l2[j]]
if j == 0 and i % 2 == 1:
# flip the first match only, every other round
# (this is because the first match always involves the last player in the list)
round.append((t2, t1))
else:
round.append((t1, t2))
s.append(round)
# rotate list by n/2, leaving last element at the end
map = map[mid:-1] + map[:mid] + map[-1:]
return s
teams = ["Lions", "Tigers", "Bears", "Dorothy", "Toto"]
schedule = create_balanced_round_robin(teams)
print("\n".join(['{} vs. {}'.format(m[0], m[1]) for round in schedule for m in round]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment