Last active
December 6, 2022 19:37
-
-
Save eltonvs/e5bb0aefb2bd83c2baab65599926419a to your computer and use it in GitHub Desktop.
Round Robin Tournament Table Generator
This file contains hidden or 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
from random import random | |
from pprint import pprint | |
def round_robin_gen(teams, away_home=False): | |
if len(teams) % 2 != 0: | |
teams.append(None) | |
matches = [] | |
for it in range(len(teams) - 1): | |
matches.append([[teams[i], teams[i + len(teams)//2]] | |
for i in range(len(teams)//2)]) | |
teams.insert(1, teams.pop()) | |
if away_home: | |
return matches + [[a[::-1] for a in m] for m in matches] | |
return matches | |
teams = [ | |
"Atlético-GO", | |
"Atlético-MG", | |
"Atlético-PR", | |
"Avaí", | |
"Bahia", | |
"Botafogo", | |
"Chapecoense", | |
"Corinthians", | |
"Coritiba", | |
"Cruzeiro", | |
"Flamengo", | |
"Fluminense", | |
"Grêmio", | |
"Palmeiras", | |
"Ponte Preta", | |
"Santos", | |
"São Paulo", | |
"Sport", | |
"Vasco", | |
"Vitória" | |
] | |
pprint(round_robin_gen(sorted(teams, key=lambda x: random()), True)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Implementação interessante meu amigo.