Skip to content

Instantly share code, notes, and snippets.

@ih84ds
Last active October 11, 2023 17:16
Show Gist options
  • Select an option

  • Save ih84ds/be485a92f334c293ce4f1c84bfba54c9 to your computer and use it in GitHub Desktop.

Select an option

Save ih84ds/be485a92f334c293ce4f1c84bfba54c9 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]))
@philipanalytixengine
Copy link
Copy Markdown

Thanks!

@ih84ds
Copy link
Copy Markdown
Author

ih84ds commented Jan 12, 2023

You're welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment