def printSchedule(teams):
combinations = []
rounds = 5
roundsArr = [set() for i in range(rounds)]
# Generate every possible match combination
for i in range(len(teams) - 1):
for j in range(i + 1, len(teams)):
combinations.append((teams[i], teams[j]))
# Store the matches that we will add in here
addedMatches = set()
# Method to determine if a certain round contains a team
def containsTeam(targetRound, team):
for match in targetRound:
leftTeam = match[0]
rightTeam = match[1]
if leftTeam == team or rightTeam == team:
return True
return False
# Method to build our schedule
def buildSchedule():
if len(addedMatches) == len(combinations):
return True
# Find which round needs a match
for roundSet in roundsArr:
if len(roundSet) < 3:
targetRound = roundSet
break
# Find which match is a possible add for the targetRound
for combination in combinations:
# If the match hasn't been added yet...
if combination not in addedMatches:
leftTeam = combination[0]
rightTeam = combination[1]
# If the targetRound is empty or the targetRound doesn't contain either team from our current match, add it
if len(targetRound) == 0 or (not containsTeam(targetRound, leftTeam) and not containsTeam(targetRound, rightTeam)):
# Found a possible match
targetRound.add(combination)
addedMatches.add(combination)
if buildSchedule():
return True
# Remove it if the recursive function returns false, find another team
addedMatches.remove(combination)
targetRound.remove(combination)
return False
# Call our scheduling function
buildSchedule()
# Print our results
for i in range(rounds):
print("-- " + "Round " + str(i + 1) + " --")
for match in roundsArr[i]:
leftTeam = match[0]
rightTeam = match[1]
print(str(leftTeam) + " vs " + str(rightTeam))
return